aboutsummaryrefslogtreecommitdiff
path: root/bootloaders/diskloader/src/ThinFAT.cpp
blob: c23874170ab1d05c80ee5dc00aff6f647ae0c84c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
/* Copyright (c) 2011, Peter Barrett  
**  
** Permission to use, copy, modify, and/or distribute this software for  
** any purpose with or without fee is hereby granted, provided that the  
** above copyright notice and this permission notice appear in all copies.  
** 
** THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL  
** WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED  
** WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR  
** BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES  
** OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,  
** WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION,  
** ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS  
** SOFTWARE.  
*/

#include "Platform.h"

#ifdef MSC_ENABLED

extern const u8 Sector_0[] PROGMEM;
extern const u8 Sector_1[] PROGMEM;
extern const u8 Sector_2[] PROGMEM;
extern const u8 Sector_3[] PROGMEM;
extern const u8 Sector_Size[] PROGMEM;


const u8 Sector_0[27] =
{
	0xEB,0x3C,0x90,
	FAT_OEM_NAME,
	0x00,0x02,	// Bytes per sector 512
	0x40,		// Sectors per Cluster 32k per cluster
	0x01,0x00,	// Reserved sectors
	0x02,		// FATSs
	0x10,0x00,	// root entries
	4+64,0x00,	// number of sectors: 30k file
	0xF8,		// Media type fixed
	0x01,0x00,	// Sectors per FAT
	0x01,0x00,	// Sectors per head
	0x01,//0x00,	// Heads per cylinder
	//0x00,0x00,0x00,0x00,	// Hidden
	//0x00,0x00,0x00,0x00,	// long number of sectors
	//0x00,0x00,				// Ext flags
	// 38 bytes
};

//	FAT12 clusters for 32k file
const u8 Sector_1[] =
{
#if 0
	0xF8,0xFF,0xFF,
	0xFF,0x0F//,0x00
#endif
};

const u8 Sector_2[] =
{
};

const u8 Sector_3[] =
{
#if 0
	FAT_FILE_NAME, 0x00,0x00,0x00,0x00,0x00,	// Uppercase name please
	0x43,0x3E,0x43,0x3E,0x00,0x00,0xAB,0x8C,0x40,0x3E,0x02,0x00,0x00,0x80,//0x00,0x00
	// 0x00,0x00,
#endif

#ifdef FAT_DISK_LABEL
	 FAT_DISK_LABEL,0x28
#endif
};

#if 0
const u8 Sector_Size[] =
{
	0,
	sizeof(Sector_0),
	sizeof(Sector_0) + sizeof(Sector_1),
	sizeof(Sector_0) + sizeof(Sector_1) + sizeof(Sector_2),
	sizeof(Sector_0) + sizeof(Sector_1) + sizeof(Sector_2) + sizeof(Sector_3)
};
#endif

const u8* LBAToROM(u8 b)
{
	b -= 4;
	return (const u8*)(b << 9);
}

void SendSector(u8 b)
{
	if (b == 0)
	{
		u8 n = sizeof(Sector_0);
		Transfer(MSC_TX | TRANSFER_PGM,Sector_0,n);
		Transfer(MSC_TX | TRANSFER_ZERO,0,512-n);
		return;
	}
	
	if ((b < 4) || (b >= 64))
	{
		Transfer(MSC_TX | TRANSFER_ZERO,0,512);
		return;
	}
	
		//	Copy body of file from ROM
	Transfer(MSC_TX | TRANSFER_PGM,LBAToROM(b),512);
}

#define GENERATE_INVALID_ROM_ADDRESS 60

void RecvSector(u8 lba)
{
	if ((lba < 4) || (lba >= 64))
		lba = GENERATE_INVALID_ROM_ADDRESS;	// Only care about the first 30k

	//	Write sectors to flash
	u16 addr = (u16)LBAToROM(lba);		// may generate out of range addresses, Program will fix
	u8 i = 4;							// 4x128 is a sector
	while (i--)
	{
		Program(MSC_RX,addr,128);
		addr += 128;
	}
}

#endif