1*d5c9a868SElliott Hughes /* Copyright 1997-2003,2006,2007,2009 Alain Knaff.
2*d5c9a868SElliott Hughes * This file is part of mtools.
3*d5c9a868SElliott Hughes *
4*d5c9a868SElliott Hughes * Mtools is free software: you can redistribute it and/or modify
5*d5c9a868SElliott Hughes * it under the terms of the GNU General Public License as published by
6*d5c9a868SElliott Hughes * the Free Software Foundation, either version 3 of the License, or
7*d5c9a868SElliott Hughes * (at your option) any later version.
8*d5c9a868SElliott Hughes *
9*d5c9a868SElliott Hughes * Mtools is distributed in the hope that it will be useful,
10*d5c9a868SElliott Hughes * but WITHOUT ANY WARRANTY; without even the implied warranty of
11*d5c9a868SElliott Hughes * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12*d5c9a868SElliott Hughes * GNU General Public License for more details.
13*d5c9a868SElliott Hughes *
14*d5c9a868SElliott Hughes * You should have received a copy of the GNU General Public License
15*d5c9a868SElliott Hughes * along with Mtools. If not, see <http://www.gnu.org/licenses/>.
16*d5c9a868SElliott Hughes *
17*d5c9a868SElliott Hughes * mlabel.c
18*d5c9a868SElliott Hughes * Make an MSDOS volume label
19*d5c9a868SElliott Hughes */
20*d5c9a868SElliott Hughes
21*d5c9a868SElliott Hughes #include "sysincludes.h"
22*d5c9a868SElliott Hughes #include "msdos.h"
23*d5c9a868SElliott Hughes #include "mainloop.h"
24*d5c9a868SElliott Hughes #include "vfat.h"
25*d5c9a868SElliott Hughes #include "mtools.h"
26*d5c9a868SElliott Hughes #include "nameclash.h"
27*d5c9a868SElliott Hughes #include "fsP.h"
28*d5c9a868SElliott Hughes
29*d5c9a868SElliott Hughes static void usage(int ret) NORETURN;
usage(int ret)30*d5c9a868SElliott Hughes static void usage(int ret)
31*d5c9a868SElliott Hughes {
32*d5c9a868SElliott Hughes fprintf(stderr,
33*d5c9a868SElliott Hughes "Mtools version %s, dated %s\n", mversion, mdate);
34*d5c9a868SElliott Hughes fprintf(stderr,
35*d5c9a868SElliott Hughes "Usage: %s [-v] drive\n", progname);
36*d5c9a868SElliott Hughes exit(ret);
37*d5c9a868SElliott Hughes }
38*d5c9a868SElliott Hughes
39*d5c9a868SElliott Hughes
displayInfosector(Stream_t * Stream,union bootsector * boot)40*d5c9a868SElliott Hughes static void displayInfosector(Stream_t *Stream, union bootsector *boot)
41*d5c9a868SElliott Hughes {
42*d5c9a868SElliott Hughes InfoSector_t *infosec;
43*d5c9a868SElliott Hughes
44*d5c9a868SElliott Hughes if(WORD(ext.fat32.infoSector) == MAX16)
45*d5c9a868SElliott Hughes return;
46*d5c9a868SElliott Hughes
47*d5c9a868SElliott Hughes infosec = (InfoSector_t *) safe_malloc(WORD(secsiz));
48*d5c9a868SElliott Hughes force_pread(Stream, (char *) infosec,
49*d5c9a868SElliott Hughes (mt_off_t) WORD(secsiz) * WORD(ext.fat32.infoSector),
50*d5c9a868SElliott Hughes WORD(secsiz));
51*d5c9a868SElliott Hughes printf("\nInfosector:\n");
52*d5c9a868SElliott Hughes printf("signature=0x%08x\n", _DWORD(infosec->signature1));
53*d5c9a868SElliott Hughes if(_DWORD(infosec->count) != MAX32)
54*d5c9a868SElliott Hughes printf("free clusters=%u\n", _DWORD(infosec->count));
55*d5c9a868SElliott Hughes if(_DWORD(infosec->pos) != MAX32)
56*d5c9a868SElliott Hughes printf("last allocated cluster=%u\n", _DWORD(infosec->pos));
57*d5c9a868SElliott Hughes }
58*d5c9a868SElliott Hughes
59*d5c9a868SElliott Hughes /*
60*d5c9a868SElliott Hughes * Number of hidden sector is only a 4 byte quantity if number of sectors is
61*d5c9a868SElliott Hughes */
getHidden(union bootsector * boot)62*d5c9a868SElliott Hughes static uint32_t getHidden(union bootsector *boot) {
63*d5c9a868SElliott Hughes return WORD(psect) ? WORD(nhs) : DWORD(nhs);
64*d5c9a868SElliott Hughes }
65*d5c9a868SElliott Hughes
displayBPB(Stream_t * Stream,union bootsector * boot)66*d5c9a868SElliott Hughes static void displayBPB(Stream_t *Stream, union bootsector *boot) {
67*d5c9a868SElliott Hughes struct label_blk_t *labelBlock;
68*d5c9a868SElliott Hughes
69*d5c9a868SElliott Hughes printf("bootsector information\n");
70*d5c9a868SElliott Hughes printf("======================\n");
71*d5c9a868SElliott Hughes printf("banner:\"%.8s\"\n", boot->boot.banner);
72*d5c9a868SElliott Hughes printf("sector size: %d bytes\n", WORD(secsiz));
73*d5c9a868SElliott Hughes printf("cluster size: %d sectors\n", boot->boot.clsiz);
74*d5c9a868SElliott Hughes printf("reserved (boot) sectors: %d\n", WORD(nrsvsect));
75*d5c9a868SElliott Hughes printf("fats: %d\n", boot->boot.nfat);
76*d5c9a868SElliott Hughes printf("max available root directory slots: %d\n",
77*d5c9a868SElliott Hughes WORD(dirents));
78*d5c9a868SElliott Hughes printf("small size: %d sectors\n", WORD(psect));
79*d5c9a868SElliott Hughes printf("media descriptor byte: 0x%x\n", boot->boot.descr);
80*d5c9a868SElliott Hughes printf("sectors per fat: %d\n", WORD(fatlen));
81*d5c9a868SElliott Hughes printf("sectors per track: %d\n", WORD(nsect));
82*d5c9a868SElliott Hughes printf("heads: %d\n", WORD(nheads));
83*d5c9a868SElliott Hughes printf("hidden sectors: %d\n", getHidden(boot));
84*d5c9a868SElliott Hughes if(!WORD(psect))
85*d5c9a868SElliott Hughes printf("big size: %u sectors\n", DWORD(bigsect));
86*d5c9a868SElliott Hughes
87*d5c9a868SElliott Hughes if(WORD(fatlen)) {
88*d5c9a868SElliott Hughes labelBlock = &boot->boot.ext.old.labelBlock;
89*d5c9a868SElliott Hughes } else {
90*d5c9a868SElliott Hughes labelBlock = &boot->boot.ext.fat32.labelBlock;
91*d5c9a868SElliott Hughes }
92*d5c9a868SElliott Hughes
93*d5c9a868SElliott Hughes if(has_BPB4) {
94*d5c9a868SElliott Hughes printf("physical drive id: 0x%x\n",
95*d5c9a868SElliott Hughes labelBlock->physdrive);
96*d5c9a868SElliott Hughes printf("reserved=0x%x\n",
97*d5c9a868SElliott Hughes labelBlock->reserved);
98*d5c9a868SElliott Hughes printf("dos4=0x%x\n",
99*d5c9a868SElliott Hughes labelBlock->dos4);
100*d5c9a868SElliott Hughes printf("serial number: %08X\n",
101*d5c9a868SElliott Hughes _DWORD(labelBlock->serial));
102*d5c9a868SElliott Hughes printf("disk label=\"%11.11s\"\n",
103*d5c9a868SElliott Hughes labelBlock->label);
104*d5c9a868SElliott Hughes printf("disk type=\"%8.8s\"\n",
105*d5c9a868SElliott Hughes labelBlock->fat_type);
106*d5c9a868SElliott Hughes }
107*d5c9a868SElliott Hughes
108*d5c9a868SElliott Hughes if(!WORD(fatlen)){
109*d5c9a868SElliott Hughes printf("Big fatlen=%u\n",
110*d5c9a868SElliott Hughes DWORD(ext.fat32.bigFat));
111*d5c9a868SElliott Hughes printf("Extended flags=0x%04x\n",
112*d5c9a868SElliott Hughes WORD(ext.fat32.extFlags));
113*d5c9a868SElliott Hughes printf("FS version=0x%04x\n",
114*d5c9a868SElliott Hughes WORD(ext.fat32.fsVersion));
115*d5c9a868SElliott Hughes printf("rootCluster=%u\n",
116*d5c9a868SElliott Hughes DWORD(ext.fat32.rootCluster));
117*d5c9a868SElliott Hughes if(WORD(ext.fat32.infoSector) != MAX16)
118*d5c9a868SElliott Hughes printf("infoSector location=%d\n",
119*d5c9a868SElliott Hughes WORD(ext.fat32.infoSector));
120*d5c9a868SElliott Hughes if(WORD(ext.fat32.backupBoot) != MAX16)
121*d5c9a868SElliott Hughes printf("backup boot sector=%d\n",
122*d5c9a868SElliott Hughes WORD(ext.fat32.backupBoot));
123*d5c9a868SElliott Hughes displayInfosector(Stream, boot);
124*d5c9a868SElliott Hughes }
125*d5c9a868SElliott Hughes }
126*d5c9a868SElliott Hughes
try(uint32_t tot_sectors,Fs_t * masterFs,Fs_t * tryFs,struct device * master_dev,struct device * try_dev,uint8_t * bootDescr)127*d5c9a868SElliott Hughes static int try(uint32_t tot_sectors, Fs_t *masterFs, Fs_t *tryFs,
128*d5c9a868SElliott Hughes struct device *master_dev, struct device *try_dev,
129*d5c9a868SElliott Hughes uint8_t *bootDescr) {
130*d5c9a868SElliott Hughes *tryFs = *masterFs;
131*d5c9a868SElliott Hughes *try_dev = *master_dev;
132*d5c9a868SElliott Hughes return calc_fs_parameters(try_dev, 0, tot_sectors,
133*d5c9a868SElliott Hughes tryFs, bootDescr);
134*d5c9a868SElliott Hughes }
135*d5c9a868SElliott Hughes
print_mformat_commandline(const char * imgFile,char drive,struct device * dev,union bootsector * boot,int media,int haveBPB)136*d5c9a868SElliott Hughes static void print_mformat_commandline(const char *imgFile,
137*d5c9a868SElliott Hughes char drive,
138*d5c9a868SElliott Hughes struct device *dev,
139*d5c9a868SElliott Hughes union bootsector *boot,
140*d5c9a868SElliott Hughes int media,
141*d5c9a868SElliott Hughes int haveBPB) {
142*d5c9a868SElliott Hughes uint8_t size_code;
143*d5c9a868SElliott Hughes uint32_t sect_per_track;
144*d5c9a868SElliott Hughes uint32_t hidden;
145*d5c9a868SElliott Hughes uint32_t tot_sectors;
146*d5c9a868SElliott Hughes int tracks_match=0;
147*d5c9a868SElliott Hughes Fs_t masterFs, tryFs, actual;
148*d5c9a868SElliott Hughes struct device used_dev;
149*d5c9a868SElliott Hughes uint8_t tryMedia;
150*d5c9a868SElliott Hughes int bad;
151*d5c9a868SElliott Hughes
152*d5c9a868SElliott Hughes sect_per_track = dev->sectors * dev->heads;
153*d5c9a868SElliott Hughes if(sect_per_track == 0)
154*d5c9a868SElliott Hughes return;
155*d5c9a868SElliott Hughes
156*d5c9a868SElliott Hughes tot_sectors = parseFsParams(&actual, boot,
157*d5c9a868SElliott Hughes media | (haveBPB ? 0x100:0),
158*d5c9a868SElliott Hughes sect_per_track);
159*d5c9a868SElliott Hughes if(tot_sectors == 0)
160*d5c9a868SElliott Hughes return;
161*d5c9a868SElliott Hughes
162*d5c9a868SElliott Hughes printf("mformat command line:\n mformat ");
163*d5c9a868SElliott Hughes
164*d5c9a868SElliott Hughes if(haveBPB) {
165*d5c9a868SElliott Hughes if(media == 0xf0)
166*d5c9a868SElliott Hughes hidden = getHidden(boot);
167*d5c9a868SElliott Hughes else
168*d5c9a868SElliott Hughes hidden = 0;
169*d5c9a868SElliott Hughes size_code = (uint8_t) actual.sectorShift-7;
170*d5c9a868SElliott Hughes } else {
171*d5c9a868SElliott Hughes size_code=2;
172*d5c9a868SElliott Hughes hidden = 0;
173*d5c9a868SElliott Hughes }
174*d5c9a868SElliott Hughes
175*d5c9a868SElliott Hughes if(tot_sectors ==
176*d5c9a868SElliott Hughes dev->tracks * sect_per_track - hidden % sect_per_track) {
177*d5c9a868SElliott Hughes tracks_match=1;
178*d5c9a868SElliott Hughes printf("-t %d ", dev->tracks);
179*d5c9a868SElliott Hughes } else {
180*d5c9a868SElliott Hughes printf("-T %d ", tot_sectors);
181*d5c9a868SElliott Hughes }
182*d5c9a868SElliott Hughes printf ("-h %d -s %d ", dev->heads, dev->sectors);
183*d5c9a868SElliott Hughes if(haveBPB && (hidden || !tracks_match))
184*d5c9a868SElliott Hughes printf("-H %d ", hidden);
185*d5c9a868SElliott Hughes used_dev=*dev;
186*d5c9a868SElliott Hughes if(size_code != 2) {
187*d5c9a868SElliott Hughes printf("-S %d ",size_code);
188*d5c9a868SElliott Hughes used_dev.ssize = size_code;
189*d5c9a868SElliott Hughes }
190*d5c9a868SElliott Hughes
191*d5c9a868SElliott Hughes initFsForFormat(&masterFs);
192*d5c9a868SElliott Hughes setFsSectorSize(&masterFs, &used_dev, 0);
193*d5c9a868SElliott Hughes
194*d5c9a868SElliott Hughes if(actual.num_fat != 2) {
195*d5c9a868SElliott Hughes masterFs.num_fat = actual.num_fat;
196*d5c9a868SElliott Hughes printf("-d %d ", actual.num_fat);
197*d5c9a868SElliott Hughes }
198*d5c9a868SElliott Hughes
199*d5c9a868SElliott Hughes bad=try(tot_sectors, &masterFs, &tryFs, dev , &used_dev, &tryMedia);
200*d5c9a868SElliott Hughes
201*d5c9a868SElliott Hughes if(bad || actual.dir_len != tryFs.dir_len) {
202*d5c9a868SElliott Hughes masterFs.dir_len = actual.dir_len;
203*d5c9a868SElliott Hughes printf("-r %d ", actual.dir_len);
204*d5c9a868SElliott Hughes bad = try(tot_sectors,
205*d5c9a868SElliott Hughes &masterFs, &tryFs, dev , &used_dev,
206*d5c9a868SElliott Hughes &tryMedia);
207*d5c9a868SElliott Hughes }
208*d5c9a868SElliott Hughes
209*d5c9a868SElliott Hughes if(bad || actual.cluster_size != tryFs.cluster_size) {
210*d5c9a868SElliott Hughes masterFs.cluster_size = actual.cluster_size;
211*d5c9a868SElliott Hughes printf("-c %d ", actual.cluster_size);
212*d5c9a868SElliott Hughes bad = try(tot_sectors,
213*d5c9a868SElliott Hughes &masterFs, &tryFs, dev , &used_dev,
214*d5c9a868SElliott Hughes &tryMedia);
215*d5c9a868SElliott Hughes }
216*d5c9a868SElliott Hughes
217*d5c9a868SElliott Hughes if(bad || actual.fat_start != tryFs.fat_start) {
218*d5c9a868SElliott Hughes masterFs.fat_start = actual.fat_start;
219*d5c9a868SElliott Hughes printf("-R %d ", actual.fat_start);
220*d5c9a868SElliott Hughes bad = try(tot_sectors,
221*d5c9a868SElliott Hughes &masterFs, &tryFs, dev , &used_dev,
222*d5c9a868SElliott Hughes &tryMedia);
223*d5c9a868SElliott Hughes }
224*d5c9a868SElliott Hughes
225*d5c9a868SElliott Hughes if(bad || actual.fat_len != tryFs.fat_len) {
226*d5c9a868SElliott Hughes masterFs.fat_len = actual.fat_len;
227*d5c9a868SElliott Hughes printf("-L %d ", actual.fat_len);
228*d5c9a868SElliott Hughes bad = try(tot_sectors,
229*d5c9a868SElliott Hughes &masterFs, &tryFs, dev , &used_dev,
230*d5c9a868SElliott Hughes &tryMedia);
231*d5c9a868SElliott Hughes }
232*d5c9a868SElliott Hughes #ifdef HAVE_ASSERT_H
233*d5c9a868SElliott Hughes assert(!bad);
234*d5c9a868SElliott Hughes #endif
235*d5c9a868SElliott Hughes if((media & 0xff) != (tryMedia & 0xff))
236*d5c9a868SElliott Hughes printf("-m %d ", (media & 0xff));
237*d5c9a868SElliott Hughes
238*d5c9a868SElliott Hughes if(actual.fat_bits == 32) {
239*d5c9a868SElliott Hughes if(actual.backupBoot != tryFs.backupBoot)
240*d5c9a868SElliott Hughes printf("-K %d ", actual.backupBoot);
241*d5c9a868SElliott Hughes }
242*d5c9a868SElliott Hughes
243*d5c9a868SElliott Hughes if(imgFile != NULL)
244*d5c9a868SElliott Hughes printf("-i \"%s\" ", imgFile);
245*d5c9a868SElliott Hughes printf("%c:\n", ch_tolower(drive));
246*d5c9a868SElliott Hughes printf("\n");
247*d5c9a868SElliott Hughes }
248*d5c9a868SElliott Hughes
249*d5c9a868SElliott Hughes
250*d5c9a868SElliott Hughes void minfo(int argc, char **argv, int type UNUSEDP) NORETURN;
minfo(int argc,char ** argv,int type UNUSEDP)251*d5c9a868SElliott Hughes void minfo(int argc, char **argv, int type UNUSEDP)
252*d5c9a868SElliott Hughes {
253*d5c9a868SElliott Hughes union bootsector boot;
254*d5c9a868SElliott Hughes
255*d5c9a868SElliott Hughes char name[EXPAND_BUF];
256*d5c9a868SElliott Hughes struct device dev;
257*d5c9a868SElliott Hughes char drive;
258*d5c9a868SElliott Hughes int verbose=0;
259*d5c9a868SElliott Hughes int c;
260*d5c9a868SElliott Hughes Stream_t *Stream;
261*d5c9a868SElliott Hughes int have_drive = 0;
262*d5c9a868SElliott Hughes int ex=0;
263*d5c9a868SElliott Hughes char *imgFile=NULL;
264*d5c9a868SElliott Hughes
265*d5c9a868SElliott Hughes if(helpFlag(argc, argv))
266*d5c9a868SElliott Hughes usage(0);
267*d5c9a868SElliott Hughes while ((c = getopt(argc, argv, "i:vh")) != EOF) {
268*d5c9a868SElliott Hughes switch (c) {
269*d5c9a868SElliott Hughes case 'i':
270*d5c9a868SElliott Hughes set_cmd_line_image(optarg);
271*d5c9a868SElliott Hughes imgFile=optarg;
272*d5c9a868SElliott Hughes break;
273*d5c9a868SElliott Hughes case 'v':
274*d5c9a868SElliott Hughes verbose = 1;
275*d5c9a868SElliott Hughes break;
276*d5c9a868SElliott Hughes case 'h':
277*d5c9a868SElliott Hughes usage(0);
278*d5c9a868SElliott Hughes default:
279*d5c9a868SElliott Hughes usage(1);
280*d5c9a868SElliott Hughes }
281*d5c9a868SElliott Hughes }
282*d5c9a868SElliott Hughes
283*d5c9a868SElliott Hughes for(;optind <= argc; optind++) {
284*d5c9a868SElliott Hughes int media;
285*d5c9a868SElliott Hughes int haveBPB;
286*d5c9a868SElliott Hughes
287*d5c9a868SElliott Hughes if(optind == argc) {
288*d5c9a868SElliott Hughes if(have_drive)
289*d5c9a868SElliott Hughes break;
290*d5c9a868SElliott Hughes drive = get_default_drive();
291*d5c9a868SElliott Hughes } else {
292*d5c9a868SElliott Hughes if(!argv[optind][0] || argv[optind][1] != ':')
293*d5c9a868SElliott Hughes usage(1);
294*d5c9a868SElliott Hughes drive = ch_toupper(argv[optind][0]);
295*d5c9a868SElliott Hughes }
296*d5c9a868SElliott Hughes have_drive = 1;
297*d5c9a868SElliott Hughes
298*d5c9a868SElliott Hughes if(! (Stream = find_device(drive, O_RDONLY, &dev, &boot,
299*d5c9a868SElliott Hughes name, &media, 0, NULL))) {
300*d5c9a868SElliott Hughes fprintf(stderr, "Could not open drive %c:\n", drive);
301*d5c9a868SElliott Hughes ex=1;
302*d5c9a868SElliott Hughes continue;
303*d5c9a868SElliott Hughes }
304*d5c9a868SElliott Hughes
305*d5c9a868SElliott Hughes haveBPB = media >= 0x100;
306*d5c9a868SElliott Hughes media = media & 0xff;
307*d5c9a868SElliott Hughes
308*d5c9a868SElliott Hughes printf("device information:\n");
309*d5c9a868SElliott Hughes printf("===================\n");
310*d5c9a868SElliott Hughes printf("filename=\"%s\"\n", name);
311*d5c9a868SElliott Hughes printf("sectors per track: %d\n", dev.sectors);
312*d5c9a868SElliott Hughes printf("heads: %d\n", dev.heads);
313*d5c9a868SElliott Hughes printf("cylinders: %d\n\n", dev.tracks);
314*d5c9a868SElliott Hughes printf("media byte: %02x\n\n", media & 0xff);
315*d5c9a868SElliott Hughes
316*d5c9a868SElliott Hughes print_mformat_commandline(imgFile, drive,
317*d5c9a868SElliott Hughes &dev, &boot, media, haveBPB);
318*d5c9a868SElliott Hughes
319*d5c9a868SElliott Hughes if(haveBPB || verbose)
320*d5c9a868SElliott Hughes displayBPB(Stream, &boot);
321*d5c9a868SElliott Hughes
322*d5c9a868SElliott Hughes if(verbose) {
323*d5c9a868SElliott Hughes uint16_t size;
324*d5c9a868SElliott Hughes ssize_t ssize;
325*d5c9a868SElliott Hughes unsigned char *buf;
326*d5c9a868SElliott Hughes
327*d5c9a868SElliott Hughes printf("\n");
328*d5c9a868SElliott Hughes size = WORD_S(secsiz);
329*d5c9a868SElliott Hughes
330*d5c9a868SElliott Hughes buf = (unsigned char *) malloc(size);
331*d5c9a868SElliott Hughes if(!buf) {
332*d5c9a868SElliott Hughes fprintf(stderr, "Out of memory error\n");
333*d5c9a868SElliott Hughes exit(1);
334*d5c9a868SElliott Hughes }
335*d5c9a868SElliott Hughes
336*d5c9a868SElliott Hughes ssize = PREADS(Stream, buf, 0, size);
337*d5c9a868SElliott Hughes if(ssize < 0) {
338*d5c9a868SElliott Hughes perror("read boot sector");
339*d5c9a868SElliott Hughes exit(1);
340*d5c9a868SElliott Hughes }
341*d5c9a868SElliott Hughes
342*d5c9a868SElliott Hughes print_sector("Boot sector hexdump", buf, (uint16_t)ssize);
343*d5c9a868SElliott Hughes }
344*d5c9a868SElliott Hughes }
345*d5c9a868SElliott Hughes FREE(&Stream);
346*d5c9a868SElliott Hughes exit(ex);
347*d5c9a868SElliott Hughes }
348