1 #include "sysincludes.h"
2 #include "mtools.h"
3
4 static struct OldDos_t old_dos[]={
5 { 40, 9, 1, 4, 1, 2, 0xfc }, /* 180 KB */
6 { 40, 9, 2, 7, 2, 2, 0xfd }, /* 360 KB */
7 { 40, 8, 1, 4, 1, 1, 0xfe }, /* 160 KB */
8 { 40, 8, 2, 7, 2, 1, 0xff }, /* 320 KB */
9 { 80, 9, 2, 7, 2, 3, 0xf9 }, /* 720 KB */
10 { 80, 15, 2,14, 1, 7, 0xf9 }, /* 1200 KB */
11 { 80, 18, 2,14, 1, 9, 0xf0 }, /* 1440 KB */
12 { 80, 36, 2,15, 2, 9, 0xf0 }, /* 2880 KB */
13
14 /* Source: https://en.wikipedia.org/w/index.php?title=File_Allocation_Table&oldid=560606333#Exceptions : */
15 /* https://www.win.tue.nl/~aeb/linux/fs/fat/fat-1.html */
16 { 80, 8, 2, 7, 2, 2, 0xfb }, /* 640 KB */
17 { 80, 8, 1, 7, 2, 2, 0xfa }, /* 320 KB */
18 { 80, 9, 1, 7, 2, 2, 0xf8 }, /* 360 KB */
19 };
20
21 /**
22 * Get Old Dos parameters for a filesystem of size KBytes (assuming
23 * 512 byte sectors), i.e. number of sectors is double the size
24 */
getOldDosBySize(size_t size)25 struct OldDos_t *getOldDosBySize(size_t size) {
26 size_t i;
27 size = size * 2;
28 for(i=0; i < sizeof(old_dos) / sizeof(old_dos[0]); i++){
29 if (old_dos[i].sectors *
30 old_dos[i].tracks *
31 old_dos[i].heads == size)
32 return &old_dos[i];
33 }
34 return NULL;
35 }
36
getOldDosByMedia(int media)37 struct OldDos_t *getOldDosByMedia(int media) {
38 size_t i;
39 for(i=0; i < sizeof(old_dos) / sizeof(old_dos[0]); i++){
40 if (old_dos[i].media == media)
41 return &old_dos[i];
42 }
43 fprintf(stderr, "Unknown media type %02x\n", media);
44 return NULL;
45 }
46
getOldDosByParams(unsigned int tracks,unsigned int heads,unsigned int sectors,unsigned int dir_len,unsigned int cluster_size)47 struct OldDos_t *getOldDosByParams(unsigned int tracks,
48 unsigned int heads,
49 unsigned int sectors,
50 unsigned int dir_len,
51 unsigned int cluster_size) {
52 size_t i;
53 for(i=0; i < sizeof(old_dos) / sizeof(old_dos[0]); i++){
54 if (sectors == old_dos[i].sectors &&
55 tracks == old_dos[i].tracks &&
56 heads == old_dos[i].heads &&
57 (dir_len == 0 || dir_len == old_dos[i].dir_len) &&
58 (cluster_size == 0 ||
59 cluster_size == old_dos[i].cluster_size)) {
60 return &old_dos[i];
61 }
62 }
63 return NULL;
64 }
65
setDeviceFromOldDos(int media,struct device * dev)66 int setDeviceFromOldDos(int media, struct device *dev) {
67 struct OldDos_t *params=getOldDosByMedia(media);
68 if(params == NULL)
69 return -1;
70 dev->heads = params->heads;
71 dev->tracks = params->tracks;
72 dev->sectors = params->sectors;
73 dev->ssize = 0x80;
74 dev->use_2m = ~1u;
75 return 0;
76 }
77