1 //---------------------------------------------------------------------------
2 //File name: part_driver.c
3 //---------------------------------------------------------------------------
6 #include "usbhd_common.h"
8 #include "part_driver.h"
10 #include "fat_driver.h"
12 //#define DEBUG //comment out this line when not debugging
14 #include "mass_debug.h"
16 #define READ_SECTOR(d, a, b) scache_readSector((d)->cache, (a), (void **)&b)
18 typedef struct _part_record
{
19 unsigned char sid
; //system id - 4=16bit FAT (16bit sector numbers)
20 // 5=extended partition
21 // 6=16bit FAT (32bit sector numbers)
22 unsigned int start
; // start sector of the partition
23 unsigned int count
; // length of the partititon (total number of sectors)
26 typedef struct _part_table
{
27 part_record record
[4]; //maximum of 4 primary partitions
30 typedef struct _part_raw_record
{
31 unsigned char active
; //Set to 80h if this partition is active / bootable
32 unsigned char startH
; //Partition's starting head.
33 unsigned char startST
[2]; //Partition's starting sector and track.
34 unsigned char sid
; //Partition's system ID number.
35 unsigned char endH
; //Partition's ending head.
36 unsigned char endST
[2]; //Partition's ending sector and track.
37 unsigned char startLBA
[4]; //Starting LBA (sector)
38 unsigned char size
[4]; //Partition size in sectors.
41 //---------------------------------------------------------------------------
42 USBHD_INLINE
void part_getPartitionRecord(part_raw_record
* raw
, part_record
* rec
)
45 rec
->start
= getI32(raw
->startLBA
);
46 rec
->count
= getI32(raw
->size
);
49 //---------------------------------------------------------------------------
50 int part_getPartitionTable(mass_dev
* dev
, part_table
* part
)
52 part_raw_record
* part_raw
;
57 ret
= READ_SECTOR(dev
, 0, sbuf
); // read sector 0 - Disk MBR or boot sector
60 printf("USBHDFSD: part_getPartitionTable read failed %d!\n", ret
);
64 printf("USBHDFSD: boot signature %X %X\n", sbuf
[0x1FE], sbuf
[0x1FF]);
65 if (sbuf
[0x1FE] == 0x55 && sbuf
[0x1FF] == 0xAA)
67 for ( i
= 0; i
< 4; i
++)
69 part_raw
= ( part_raw_record
* )( sbuf
+ 0x01BE + ( i
* 16 ) );
70 part_getPartitionRecord(part_raw
, &part
->record
[i
]);
76 for ( i
= 0; i
< 4; i
++)
78 part
->record
[i
].sid
= 0;;
84 //---------------------------------------------------------------------------
85 int part_connect(mass_dev
* dev
)
90 XPRINTF("USBHDFSD: part_connect devId %i \n", dev
->devId
);
92 if (part_getPartitionTable(dev
, &partTable
) < 0)
95 for ( i
= 0; i
< 4; i
++)
98 partTable
.record
[ i
].sid
== 6 ||
99 partTable
.record
[ i
].sid
== 4 ||
100 partTable
.record
[ i
].sid
== 1 || // fat 16, fat 12
101 partTable
.record
[ i
].sid
== 0x0B ||
102 partTable
.record
[ i
].sid
== 0x0C || // fat 32
103 partTable
.record
[ i
].sid
== 0x0E) // fat 16 LBA
105 XPRINTF("USBHDFSD: mount partition %d\n", i
);
106 if (fat_mount(dev
, partTable
.record
[i
].start
, partTable
.record
[i
].count
) >= 0)
112 { // no partition table detected
113 // try to use "floppy" option
114 printf("USBHDFSD: mount drive\n");
115 if (fat_mount(dev
, 0, dev
->maxLBA
) < 0)
122 //---------------------------------------------------------------------------
123 void part_disconnect(mass_dev
* dev
)
125 printf("USBHDFSD: part_disconnect devId %i \n", dev
->devId
);
126 fat_forceUnmount(dev
);
129 //---------------------------------------------------------------------------
130 //End of file: part_driver.c
131 //---------------------------------------------------------------------------