summaryrefslogtreecommitdiff
path: root/mformat.c
diff options
context:
space:
mode:
authorDongHun Kwak <dh0128.kwak@samsung.com>2022-01-14 10:51:41 +0900
committerDongHun Kwak <dh0128.kwak@samsung.com>2022-01-14 10:51:41 +0900
commit28036bce31eb21d994194aa0645639a765e49e60 (patch)
tree80c4846dc469384ec09e7c51b5bfdad16538c9e1 /mformat.c
parentf905f5aada0700ef4be96749655f46a79d912c35 (diff)
downloadmtools-28036bce31eb21d994194aa0645639a765e49e60.tar.gz
mtools-28036bce31eb21d994194aa0645639a765e49e60.tar.bz2
mtools-28036bce31eb21d994194aa0645639a765e49e60.zip
Imported Upstream version 4.0.23upstream/4.0.23
Diffstat (limited to 'mformat.c')
-rw-r--r--mformat.c132
1 files changed, 75 insertions, 57 deletions
diff --git a/mformat.c b/mformat.c
index 7aecd2d..b3c6098 100644
--- a/mformat.c
+++ b/mformat.c
@@ -55,14 +55,12 @@
static int init_geometry_boot(union bootsector *boot, struct device *dev,
- int sectors0, int rate_0, int rate_any,
- unsigned long *tot_sectors, int keepBoot)
+ uint8_t sectors0,
+ uint8_t rate_0, uint8_t rate_any,
+ unsigned long *tot_sectors, int keepBoot)
{
- int i;
int nb_renum;
int sector2;
- int size2;
- int j;
int sum;
set_word(boot->boot.nsect, dev->sectors);
@@ -72,16 +70,22 @@ static int init_geometry_boot(union bootsector *boot, struct device *dev,
assert(*tot_sectors != 0);
#endif
- if (*tot_sectors < 0x10000){
- set_word(boot->boot.psect, *tot_sectors);
+ if (*tot_sectors <= UINT16_MAX){
+ set_word(boot->boot.psect, (uint16_t) *tot_sectors);
set_dword(boot->boot.bigsect, 0);
- } else {
+ } else if(*tot_sectors <= UINT32_MAX){
set_word(boot->boot.psect, 0);
- set_dword(boot->boot.bigsect, *tot_sectors);
+ set_dword(boot->boot.bigsect, (uint32_t) *tot_sectors);
+ } else {
+ fprintf(stderr, "Too many sectors %ld\n", *tot_sectors);
+ exit(1);
}
if (dev->use_2m & 0x7f){
int bootOffset;
+ uint8_t j;
+ uint8_t size2;
+ uint16_t i;
strncpy(boot->boot.banner, "2M-STV04", 8);
boot->boot.ext.old.res_2m = 0;
boot->boot.ext.old.fmt_2mf = 6;
@@ -285,9 +289,9 @@ static __inline__ void format_root(Fs_t *Fs, char *label, union bootsector *boot
{
Stream_t *RootDir;
char *buf;
- int i;
+ unsigned int i;
struct ClashHandling_t ch;
- int dirlen;
+ unsigned int dirlen;
init_clash_handling(&ch);
ch.name_converter = label_name_uc;
@@ -361,7 +365,7 @@ static void calc_fat_size(Fs_t *Fs, unsigned long tot_sectors)
unsigned long real_rem_sect;
unsigned long numerator;
unsigned long denominator;
- int fat_nybbles;
+ unsigned int fat_nybbles;
unsigned int slack;
int printGrowMsg=1; /* Should we print "growing FAT" messages ?*/
@@ -587,7 +591,10 @@ static void calc_cluster_size(struct Fs_t *Fs, unsigned long tot_sectors,
}
-static int old_dos_size_to_geom(size_t size, int *cyls, int *heads, int *sects)
+static int old_dos_size_to_geom(size_t size,
+ unsigned int *cyls,
+ unsigned short *heads,
+ unsigned short *sects)
{
struct OldDos_t *params = getOldDosBySize(size);
if(params != NULL) {
@@ -663,6 +670,7 @@ static void calc_fs_parameters(struct device *dev, unsigned long tot_sectors,
static void calc_fs_parameters_32(unsigned long tot_sectors,
struct Fs_t *Fs, union bootsector *boot)
{
+ unsigned long num_clus;
if(DWORD(nhs))
boot->boot.descr = 0xf8;
else
@@ -671,12 +679,13 @@ static void calc_fs_parameters_32(unsigned long tot_sectors,
if(!Fs->cluster_size)
calc_cluster_size(Fs, tot_sectors, 32);
Fs->dir_len = 0;
- Fs->num_clus = tot_sectors / Fs->cluster_size;
+ num_clus = tot_sectors / Fs->cluster_size;
/* Maximal number of clusters on FAT32 is 0xffffff6 */
- if (Fs->num_clus > 0xffffff6) {
+ if (num_clus > 0xffffff6) {
fprintf(stderr, "Too many clusters\n");
exit(1);
}
+ Fs->num_clus = (unsigned int) num_clus;
set_fat32(Fs);
calc_fat_size(Fs, tot_sectors);
set_word(boot->boot.fatlen, 0);
@@ -726,9 +735,9 @@ static int get_block_geom(int fd, struct device *dev, char *errmsg) {
struct hd_geometry geom;
int sec_size;
long size;
- int heads=dev->heads;
- int sectors=dev->sectors;
- int sect_per_track;
+ uint16_t heads=dev->heads;
+ uint16_t sectors=dev->sectors;
+ unsigned int sect_per_track;
if (ioctl(fd, HDIO_GETGEO, &geom) < 0) {
sprintf(errmsg, "Could not get geometry of device (%s)",
@@ -757,11 +766,11 @@ static int get_block_geom(int fd, struct device *dev, char *errmsg) {
sect_per_track = heads * sectors;
if(!dev->hidden) {
- int hidden;
+ unsigned long hidden;
hidden = geom.start % sect_per_track;
if(hidden && hidden != sectors) {
sprintf(errmsg,
- "Hidden (%d) does not match sectors (%d)\n",
+ "Hidden (%ld) does not match sectors (%d)\n",
hidden, sectors);
return -1;
}
@@ -856,7 +865,7 @@ static int get_lba_geom(Stream_t *Direct, unsigned long tot_sectors, struct devi
return -1;
}
- dev->tracks = tracks;
+ dev->tracks = (uint32_t) tracks;
return 0;
}
@@ -866,13 +875,14 @@ void mformat(int argc, char **argv, int dummy UNUSEDP)
{
int r; /* generic return value */
Fs_t Fs;
- int hs, hs_set;
- int arguse_2m = 0;
- int sectors0=18; /* number of sectors on track 0 */
+ unsigned int hs;
+ int hs_set;
+ unsigned int arguse_2m = 0;
+ uint8_t sectors0=18; /* number of sectors on track 0 */
int create = 0;
- int rate_0, rate_any;
+ uint8_t rate_0, rate_any;
int mangled;
- int argssize=0; /* sector size */
+ uint8_t argssize=0; /* sector size */
int msize=0;
int fat32 = 0;
struct label_blk_t *labelBlock;
@@ -888,7 +898,8 @@ void mformat(int argc, char **argv, int dummy UNUSEDP)
int c;
int keepBoot = 0;
struct device used_dev;
- int argtracks, argheads, argsectors;
+ unsigned int argtracks;
+ uint16_t argheads, argsectors;
unsigned long tot_sectors=0;
int blocksize;
@@ -898,9 +909,9 @@ void mformat(int argc, char **argv, int dummy UNUSEDP)
dos_name_t shortlabel;
struct device *dev;
- char errmsg[200];
+ char errmsg[2100];
- unsigned long serial;
+ uint32_t serial;
int serial_set;
int fsVersion;
int mediaDesc=-1;
@@ -931,14 +942,14 @@ void mformat(int argc, char **argv, int dummy UNUSEDP)
Fs.refs = 1;
Fs.dir_len = 0;
if(getenv("MTOOLS_DIR_LEN")) {
- Fs.dir_len = atoi(getenv("MTOOLS_DIR_LEN"));
+ Fs.dir_len = atoui(getenv("MTOOLS_DIR_LEN"));
if(Fs.dir_len <= 0)
Fs.dir_len=0;
}
Fs.fat_len = 0;
Fs.num_fat = 2;
if(getenv("MTOOLS_NFATS")) {
- Fs.num_fat = atoi(getenv("MTOOLS_NFATS"));
+ Fs.num_fat = atoui(getenv("MTOOLS_NFATS"));
if(Fs.num_fat <= 0)
Fs.num_fat=2;
}
@@ -971,7 +982,7 @@ void mformat(int argc, char **argv, int dummy UNUSEDP)
argtracks = 40;
break;
case 'f':
- r=old_dos_size_to_geom(atoi(optarg),
+ r=old_dos_size_to_geom(atoul(optarg),
&argtracks, &argheads,
&argsectors);
if(r) {
@@ -981,16 +992,16 @@ void mformat(int argc, char **argv, int dummy UNUSEDP)
}
break;
case 't':
- argtracks = atoi(optarg);
+ argtracks = atou16(optarg);
break;
case 'T':
- tot_sectors = atoi(optarg);
+ tot_sectors = atoui(optarg);
break;
case 'n': /*non-standard*/
case 's':
- argsectors = atoi(optarg);
+ argsectors = atou16(optarg);
break;
case 'l': /* non-standard */
@@ -1017,7 +1028,7 @@ void mformat(int argc, char **argv, int dummy UNUSEDP)
case 'S':
- argssize = atoi(optarg) | 0x80;
+ argssize = atou8(optarg) | 0x80;
if(argssize < 0x80)
usage(1);
if(argssize >= 0x87) {
@@ -1034,17 +1045,17 @@ void mformat(int argc, char **argv, int dummy UNUSEDP)
case '2':
arguse_2m = 0xff;
- sectors0 = atoi(optarg);
+ sectors0 = atou8(optarg);
break;
case '3':
arguse_2m = 0x80;
break;
case '0': /* rate on track 0 */
- rate_0 = atoi(optarg);
+ rate_0 = atou8(optarg);
break;
case 'A': /* rate on other tracks */
- rate_any = atoi(optarg);
+ rate_any = atou8(optarg);
break;
case 'M':
@@ -1059,7 +1070,7 @@ void mformat(int argc, char **argv, int dummy UNUSEDP)
break;
case 'N':
- serial = strtoul(optarg,&endptr,16);
+ serial = strtou32(optarg,&endptr,16);
serial_set = 1;
break;
case 'a': /* Atari style serial number */
@@ -1071,23 +1082,23 @@ void mformat(int argc, char **argv, int dummy UNUSEDP)
break;
case 'H':
- hs = atoi(optarg);
+ hs = atoui(optarg);
hs_set = 1;
break;
case 'I':
- fsVersion = strtoul(optarg,&endptr,0);
+ fsVersion = strtoi(optarg,&endptr,0);
break;
case 'c':
- Fs.cluster_size = atoi(optarg);
+ Fs.cluster_size = atoui(optarg);
break;
case 'r':
- Fs.dir_len = strtoul(optarg,&endptr,0);
+ Fs.dir_len = strtoui(optarg,&endptr,0);
break;
case 'L':
- Fs.fat_len = strtoul(optarg,&endptr,0);
+ Fs.fat_len = strtoui(optarg,&endptr,0);
break;
@@ -1098,7 +1109,7 @@ void mformat(int argc, char **argv, int dummy UNUSEDP)
keepBoot = 1;
break;
case 'K':
- backupBoot = atoi(optarg);
+ backupBoot = atoui(optarg);
backupBootSet=1;
if(backupBoot < 2) {
fprintf(stderr, "Backupboot must be greater than 2\n");
@@ -1106,18 +1117,18 @@ void mformat(int argc, char **argv, int dummy UNUSEDP)
}
break;
case 'R':
- resvSects = atoi(optarg);
+ resvSects = atoui(optarg);
break;
case 'h':
- argheads = atoi(optarg);
+ argheads = atou16(optarg);
break;
case 'd':
- Fs.num_fat = atoi(optarg);
+ Fs.num_fat = atoui(optarg);
break;
case 'm':
- mediaDesc = strtoul(optarg,&endptr,0);
+ mediaDesc = strtoi(optarg,&endptr,0);
if(*endptr)
- mediaDesc = strtoul(optarg,&endptr,16);
+ mediaDesc = strtoi(optarg,&endptr,16);
break;
default:
usage(1);
@@ -1283,9 +1294,15 @@ void mformat(int argc, char **argv, int dummy UNUSEDP)
if (!create &&
READS(Fs.Direct, &boot.characters, 0, Fs.sector_size) !=
(signed int) Fs.sector_size) {
+#ifdef HAVE_SNPRINTF
+ snprintf(errmsg, sizeof(errmsg)-1,
+ "Error reading from '%s', wrong parameters?",
+ name);
+#else
sprintf(errmsg,
"Error reading from '%s', wrong parameters?",
name);
+#endif
continue;
}
break;
@@ -1441,14 +1458,15 @@ void mformat(int argc, char **argv, int dummy UNUSEDP)
labelBlock->dos4 = 0x29;
if (!serial_set || Atari)
- srandom(time (0));
+ init_random();
if (!serial_set)
- serial=random();
+ serial=(uint32_t) random();
set_dword(labelBlock->serial, serial);
label_name_pc(GET_DOSCONVERT((Stream_t *)&Fs),
label[0] ? label : "NO NAME ", 0,
&mangled, &shortlabel);
- strncpy(labelBlock->label, shortlabel.base, 11);
+ strncpy(labelBlock->label, shortlabel.base, 8);
+ strncpy(labelBlock->label+8, shortlabel.ext, 3);
sprintf(labelBlock->fat_type, "FAT%2.2d ", Fs.fat_bits);
labelBlock->fat_type[7] = ' ';
@@ -1465,9 +1483,9 @@ void mformat(int argc, char **argv, int dummy UNUSEDP)
}
if(Atari) {
boot.boot.banner[4] = 0;
- boot.boot.banner[5] = random();
- boot.boot.banner[6] = random();
- boot.boot.banner[7] = random();
+ boot.boot.banner[5] = (char) random();
+ boot.boot.banner[6] = (char) random();
+ boot.boot.banner[7] = (char) random();
}
if(!keepBoot)