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