Merge remote branch 'erwan/master_new'
[profile/ivi/syslinux.git] / dos / syslinux.c
1 /* ----------------------------------------------------------------------- *
2  *
3  *   Copyright 1998-2008 H. Peter Anvin - All Rights Reserved
4  *   Copyright 2009 Intel Corporation; author: H. Peter Anvin
5  *
6  *   This program is free software; you can redistribute it and/or modify
7  *   it under the terms of the GNU General Public License as published by
8  *   the Free Software Foundation, Inc., 53 Temple Place Ste 330,
9  *   Boston MA 02111-1307, USA; either version 2 of the License, or
10  *   (at your option) any later version; incorporated herein by reference.
11  *
12  * ----------------------------------------------------------------------- */
13
14 /*
15  * syslinux.c - Linux installer program for SYSLINUX
16  *
17  * Hacked up for DOS.
18  */
19
20 #include <errno.h>
21 #include <stdio.h>
22 #include <string.h>
23 #include <stdlib.h>
24 #include <stdarg.h>
25 #include "mystuff.h"
26
27 #include "syslinux.h"
28 #include "libfat.h"
29 #include "setadv.h"
30
31 const char *program = "syslinux";       /* Name of program */
32 uint16_t dos_version;
33
34 #ifdef DEBUG
35 # define dprintf printf
36 void pause(void)
37 {
38     uint16_t ax;
39     
40     asm volatile("int $0x16" : "=a" (ax) : "a" (0));
41 }
42 #else
43 # define dprintf(...) ((void)0)
44 # define pause() ((void)0)
45 #endif
46
47 void __attribute__ ((noreturn)) usage(void)
48 {
49     puts("Usage: syslinux [-sfmar][-d directory] <drive>: [bootsecfile]\n");
50     exit(1);
51 }
52
53 void unlock_device(int);
54
55 void __attribute__ ((noreturn)) die(const char *msg)
56 {
57     unlock_device(0);
58     puts("syslinux: ");
59     puts(msg);
60     putchar('\n');
61     exit(1);
62 }
63
64 void warning(const char *msg)
65 {
66     puts("syslinux: warning: ");
67     puts(msg);
68     putchar('\n');
69 }
70
71 /*
72  * read/write wrapper functions
73  */
74 int creat(const char *filename, int mode)
75 {
76     uint16_t rv;
77     uint8_t err;
78
79     dprintf("creat(\"%s\", 0x%x)\n", filename, mode);
80
81     rv = 0x3C00;
82     asm volatile ("int $0x21 ; setc %0"
83                   : "=bcdm" (err), "+a" (rv)
84                   : "c" (mode), "d" (filename));
85     if (err) {
86         dprintf("rv = %d\n", rv);
87         die("cannot open ldlinux.sys");
88     }
89
90     return rv;
91 }
92
93 void close(int fd)
94 {
95     uint16_t rv = 0x3E00;
96
97     dprintf("close(%d)\n", fd);
98
99     asm volatile ("int $0x21":"+a" (rv)
100                   :"b"(fd));
101
102     /* The only error MS-DOS returns for close is EBADF,
103        and we really don't care... */
104 }
105
106 int rename(const char *oldname, const char *newname)
107 {
108     uint16_t rv = 0x5600;       /* Also support 43FFh? */
109     uint8_t err;
110
111     dprintf("rename(\"%s\", \"%s\")\n", oldname, newname);
112
113     asm volatile ("int $0x21 ; setc %0":"=bcdm" (err), "+a"(rv)
114                   :"d"(oldname), "D"(newname));
115
116     if (err) {
117         dprintf("rv = %d\n", rv);
118         warning("cannot move ldlinux.sys");
119         return rv;
120     }
121
122     return 0;
123 }
124
125 extern const char __payload_sseg[];
126 uint16_t ldlinux_seg;
127
128 ssize_t write_ldlinux(int fd)
129 {
130     uint32_t offset = 0;
131     uint16_t rv;
132     uint8_t err;
133
134     while (offset < syslinux_ldlinux_len) {
135         uint32_t chunk = syslinux_ldlinux_len - offset;
136         if (chunk > 32768)
137             chunk = 32768;
138         asm volatile ("pushw %%ds ; "
139                       "movw %6,%%ds ; "
140                       "int $0x21 ; "
141                       "popw %%ds ; " "setc %0":"=bcdm" (err), "=a"(rv)
142                       :"a"(0x4000), "b"(fd), "c"(chunk), "d"(offset & 15),
143                       "SD"((uint16_t) (ldlinux_seg + (offset >> 4))));
144         if (err || rv == 0)
145             die("file write error");
146         offset += rv;
147     }
148
149     return offset;
150 }
151
152 ssize_t write_file(int fd, const void *buf, size_t count)
153 {
154     uint16_t rv;
155     ssize_t done = 0;
156     uint8_t err;
157
158     dprintf("write_file(%d,%p,%u)\n", fd, buf, count);
159
160     while (count) {
161         asm volatile ("int $0x21 ; setc %0":"=bcdm" (err), "=a"(rv)
162                       :"a"(0x4000), "b"(fd), "c"(count), "d"(buf));
163         if (err || rv == 0)
164             die("file write error");
165
166         done += rv;
167         count -= rv;
168     }
169
170     return done;
171 }
172
173 static inline __attribute__ ((const))
174 uint16_t data_segment(void)
175 {
176     uint16_t ds;
177
178     asm("movw %%ds,%0" : "=rm"(ds));
179     return ds;
180 }
181
182 void write_device(int drive, const void *buf, size_t nsecs, unsigned int sector)
183 {
184     uint16_t errnum;
185     struct diskio dio;
186
187     dprintf("write_device(%d,%p,%u,%u)\n", drive, buf, nsecs, sector);
188
189     dio.startsector = sector;
190     dio.sectors = nsecs;
191     dio.bufoffs = (uintptr_t) buf;
192     dio.bufseg = data_segment();
193
194     /* Try FAT32-aware system call first */
195     asm volatile("int $0x21 ; jc 1f ; xorw %0,%0\n"
196                  "1:"
197                  : "=a" (errnum)
198                  : "a" (0x7305), "b" (&dio), "c" (-1), "d" (drive),
199                    "S" (1), "m" (dio)
200                  : "memory");
201
202     /* If not supported, try the legacy system call (int2526.S) */
203     if (errnum == 0x0001)
204         errnum = int26_write_sector(drive, &dio);
205
206     if (errnum) {
207         dprintf("rv = %04x\n", errnum);
208         die("sector write error");
209     }
210 }
211
212 void read_device(int drive, const void *buf, size_t nsecs, unsigned int sector)
213 {
214     uint16_t errnum;
215     struct diskio dio;
216
217     dprintf("read_device(%d,%p,%u,%u)\n", drive, buf, nsecs, sector);
218
219     dio.startsector = sector;
220     dio.sectors = nsecs;
221     dio.bufoffs = (uintptr_t) buf;
222     dio.bufseg = data_segment();
223
224     /* Try FAT32-aware system call first */
225     asm volatile("int $0x21 ; jc 1f ; xorw %0,%0\n"
226                  "1:"
227                  : "=a" (errnum)
228                  : "a" (0x7305), "b" (&dio), "c" (-1), "d" (drive),
229                    "S" (0), "m" (dio));
230
231     /* If not supported, try the legacy system call (int2526.S) */
232     if (errnum == 0x0001)
233         errnum = int25_read_sector(drive, &dio);
234
235     if (errnum) {
236         dprintf("rv = %04x\n", errnum);
237         die("sector read error");
238     }
239 }
240
241 /* Both traditional DOS and FAT32 DOS return this structure, but
242    FAT32 return a lot more data, so make sure we have plenty of space */
243 struct deviceparams {
244     uint8_t specfunc;
245     uint8_t devtype;
246     uint16_t devattr;
247     uint16_t cylinders;
248     uint8_t mediatype;
249     uint16_t bytespersec;
250     uint8_t secperclust;
251     uint16_t ressectors;
252     uint8_t fats;
253     uint16_t rootdirents;
254     uint16_t sectors;
255     uint8_t media;
256     uint16_t fatsecs;
257     uint16_t secpertrack;
258     uint16_t heads;
259     uint32_t hiddensecs;
260     uint32_t hugesectors;
261     uint8_t lotsofpadding[224];
262 } __attribute__ ((packed));
263
264 uint32_t get_partition_offset(int drive)
265 {
266     uint8_t err;
267     uint16_t rv;
268     struct deviceparams dp;
269
270     dp.specfunc = 1;            /* Get current information */
271
272     rv = 0x440d;
273     asm volatile ("int $0x21 ; setc %0"
274                   :"=abcdm" (err), "+a"(rv), "=m"(dp)
275                   :"b" (drive), "c" (0x0860), "d" (&dp));
276
277     if (!err)
278         return dp.hiddensecs;
279
280     rv = 0x440d;
281     asm volatile ("int $0x21 ; setc %0"
282                   : "=abcdm" (err), "+a" (rv), "=m" (dp)
283                   : "b" (drive), "c" (0x4860), "d" (&dp));
284
285     if (!err)
286         return dp.hiddensecs;
287
288     die("could not find partition start offset");
289 }
290
291 struct rwblock {
292     uint8_t special;
293     uint16_t head;
294     uint16_t cylinder;
295     uint16_t firstsector;
296     uint16_t sectors;
297     uint16_t bufferoffset;
298     uint16_t bufferseg;
299 } __attribute__ ((packed));
300
301 static struct rwblock mbr = {
302     .special = 0,
303     .head = 0,
304     .cylinder = 0,
305     .firstsector = 0,           /* MS-DOS, unlike the BIOS, zero-base sectors */
306     .sectors = 1,
307     .bufferoffset = 0,
308     .bufferseg = 0
309 };
310
311 void write_mbr(int drive, const void *buf)
312 {
313     uint16_t rv;
314     uint8_t err;
315
316     dprintf("write_mbr(%d,%p)", drive, buf);
317
318     mbr.bufferoffset = (uintptr_t) buf;
319     mbr.bufferseg = data_segment();
320
321     rv = 0x440d;
322     asm volatile ("int $0x21 ; setc %0" : "=bcdm" (err), "+a"(rv)
323                   :"c"(0x0841), "d"(&mbr), "b"(drive), "m"(mbr));
324
325     dprintf(" rv(0841) = %04x", rv);
326     if (!err) {
327         dprintf("\n");
328         return;
329     }
330
331     rv = 0x440d;
332     asm volatile ("int $0x21 ; setc %0" : "=bcdm" (err), "+a"(rv)
333                   :"c"(0x4841), "d"(&mbr), "b"(drive), "m"(mbr));
334
335     dprintf(" rv(4841) = %04x\n", rv);
336     if (err)
337         die("mbr write error");
338 }
339
340 void read_mbr(int drive, const void *buf)
341 {
342     uint16_t rv;
343     uint8_t err;
344
345     dprintf("read_mbr(%d,%p)", drive, buf);
346
347     mbr.bufferoffset = (uintptr_t) buf;
348     mbr.bufferseg = data_segment();
349
350     rv = 0x440d;
351     asm volatile ("int $0x21 ; setc %0":"=abcdm" (err), "+a"(rv)
352                   :"c"(0x0861), "d"(&mbr), "b"(drive), "m"(mbr));
353
354     dprintf(" rv(0861) = %04x", rv);
355     if (!err) {
356         dprintf("\n");
357         return;
358     }
359
360     rv = 0x440d;
361     asm volatile ("int $0x21 ; setc %0":"=abcdm" (err), "+a"(rv)
362                   :"c"(0x4861), "d"(&mbr), "b"(drive), "m"(mbr));
363
364     dprintf(" rv(4841) = %04x\n", rv);
365     if (err)
366         die("mbr read error");
367
368     dprintf("Bytes: %02x %02x %02x %02x %02x %02x %02x %02x\n",
369             ((const uint8_t *)buf)[0],
370             ((const uint8_t *)buf)[1],
371             ((const uint8_t *)buf)[2],
372             ((const uint8_t *)buf)[3],
373             ((const uint8_t *)buf)[4],
374             ((const uint8_t *)buf)[5],
375             ((const uint8_t *)buf)[6],
376             ((const uint8_t *)buf)[7]);
377 }
378
379 /* This call can legitimately fail, and we don't care, so ignore error return */
380 void set_attributes(const char *file, int attributes)
381 {
382     uint16_t rv = 0x4301;
383
384     dprintf("set_attributes(\"%s\", 0x%02x)\n", file, attributes);
385
386     asm volatile ("int $0x21":"+a" (rv)
387                   :"c"(attributes), "d"(file));
388 }
389
390 /*
391  * Version of the read_device function suitable for libfat
392  */
393 int libfat_xpread(intptr_t pp, void *buf, size_t secsize,
394                   libfat_sector_t sector)
395 {
396     read_device(pp, buf, 1, sector);
397     return secsize;
398 }
399
400 static inline void get_dos_version(void)
401 {
402     uint16_t ver;
403
404     asm("int $0x21 ; xchgb %%ah,%%al"
405         : "=a" (ver) 
406         : "a" (0x3001)
407         : "ebx", "ecx");
408     dos_version = ver;
409
410     dprintf("DOS version %d.%d\n", (dos_version >> 8), dos_version & 0xff);
411 }
412
413 /* The locking interface relies on static variables.  A massive hack :( */
414 static uint8_t lock_level, lock_drive;
415
416 static inline void set_lock_device(uint8_t device)
417 {
418     lock_level  = 0;
419     lock_drive = device;
420 }
421
422 static int do_lock(uint8_t level)
423 {
424     uint16_t level_arg = lock_drive + (level << 8);
425     uint16_t rv;
426     uint8_t err;
427 #if 0
428     /* DOS 7.10 = Win95 OSR2 = first version with FAT32 */
429     uint16_t lock_call = (dos_version >= 0x070a) ? 0x484A : 0x084A;
430 #else
431     uint16_t lock_call = 0x084A; /* MSDN says this is OK for all filesystems */
432 #endif
433
434     dprintf("Trying lock %04x... ", level_arg);
435     asm volatile ("int $0x21 ; setc %0"
436                   : "=bcdm" (err), "=a" (rv)
437                   : "a" (0x440d), "b" (level_arg),
438                     "c" (lock_call), "d" (0x0001));
439     dprintf("%s %04x\n", err ? "err" : "ok", rv);
440
441     return err ? rv : 0;
442 }
443
444 void lock_device(int level)
445 {
446     static int hard_lock = 0;
447     int err;
448
449     if (dos_version < 0x0700)
450         return;                 /* Win9x/NT only */
451
452     if (!hard_lock) {
453         /* Assume hierarchial "soft" locking supported */
454
455         while (lock_level < level) {
456             int new_level = lock_level + 1;
457             err = do_lock(new_level);
458             if (err) {
459                 if (err == 0x0001) {
460                     /* Try hard locking next */
461                     hard_lock = 1;
462                 }
463                 goto soft_fail;
464             }
465
466             lock_level = new_level;
467         }
468         return;
469     }
470
471 soft_fail:
472     if (hard_lock) {
473         /* Hard locking, only level 4 supported */
474         /* This is needed for Win9x in DOS mode */
475         
476         err = do_lock(4);
477         if (err) {
478             if (err == 0x0001) {
479                 /* Assume locking is not needed */
480                 return;
481             }
482             goto hard_fail;
483         }
484
485         lock_level = 4;
486         return;
487     }
488
489 hard_fail:
490     die("could not lock device");
491 }
492
493 void unlock_device(int level)
494 {
495     uint16_t rv;
496     uint8_t err;
497     uint16_t unlock_call;
498
499     if (dos_version < 0x0700)
500         return;                 /* Win9x/NT only */
501
502 #if 0
503     /* DOS 7.10 = Win95 OSR2 = first version with FAT32 */
504     unlock_call = (dos_version >= 0x070a) ? 0x486A : 0x086A;
505 #else
506     unlock_call = 0x086A;       /* MSDN says this is OK for all filesystems */
507 #endif
508
509     if (lock_level == 4 && level > 0)
510         return;                 /* Only drop the hard lock at the end */
511
512     while (lock_level > level) {
513         uint8_t new_level = (lock_level == 4) ? 0 : lock_level - 1;
514         uint16_t level_arg = (new_level << 8) + lock_drive;
515         rv = 0x440d;
516         dprintf("Trying unlock %04x... ", new_level);
517         asm volatile ("int $0x21 ; setc %0"
518                       : "=bcdm" (err), "+a" (rv)
519                       : "b" (level_arg), "c" (unlock_call));
520         dprintf("%s %04x\n", err ? "err" : "ok", rv);
521         lock_level = new_level;
522     }
523 }
524
525 /*
526  * This function does any desired MBR manipulation; called with the device lock held.
527  */
528 struct mbr_entry {
529     uint8_t active;             /* Active flag */
530     uint8_t bhead;              /* Begin head */
531     uint8_t bsector;            /* Begin sector */
532     uint8_t bcylinder;          /* Begin cylinder */
533     uint8_t filesystem;         /* Filesystem value */
534     uint8_t ehead;              /* End head */
535     uint8_t esector;            /* End sector */
536     uint8_t ecylinder;          /* End cylinder */
537     uint32_t startlba;          /* Start sector LBA */
538     uint32_t sectors;           /* Length in sectors */
539 } __attribute__ ((packed));
540
541 static void adjust_mbr(int device, int writembr, int set_active)
542 {
543     static unsigned char sectbuf[SECTOR_SIZE];
544     int i;
545
546     if (!writembr && !set_active)
547         return;                 /* Nothing to do */
548
549     read_mbr(device, sectbuf);
550
551     if (writembr) {
552         memcpy(sectbuf, syslinux_mbr, syslinux_mbr_len);
553         *(uint16_t *) (sectbuf + 510) = 0xaa55;
554     }
555
556     if (set_active) {
557         uint32_t offset = get_partition_offset(device);
558         struct mbr_entry *me = (struct mbr_entry *)(sectbuf + 446);
559         int found = 0;
560
561         dprintf("Searching for partition offset: %08x\n", offset);
562
563         for (i = 0; i < 4; i++) {
564             if (me->startlba == offset) {
565                 me->active = 0x80;
566                 found++;
567             } else {
568                 me->active = 0;
569             }
570             me++;
571         }
572
573         if (found < 1) {
574             die("partition not found (-a is not implemented for logical partitions)");
575         } else if (found > 1) {
576             die("multiple aliased partitions found");
577         }
578     }
579
580     write_mbr(device, sectbuf);
581 }
582
583 int main(int argc, char *argv[])
584 {
585     static unsigned char sectbuf[SECTOR_SIZE];
586     int dev_fd, fd;
587     static char ldlinux_name[] = "@:\\ldlinux.sys";
588     char **argp, *opt;
589     int force = 0;              /* -f (force) option */
590     struct libfat_filesystem *fs;
591     libfat_sector_t s, *secp;
592     libfat_sector_t *sectors;
593     int ldlinux_sectors;
594     int32_t ldlinux_cluster;
595     int nsectors;
596     const char *device = NULL, *bootsecfile = NULL;
597     const char *errmsg;
598     int i;
599     int writembr = 0;           /* -m (write MBR) option */
600     int set_active = 0;         /* -a (set partition active) option */
601     const char *subdir = NULL;
602     int stupid = 0;
603     int raid_mode = 0;
604     int patch_sectors;
605
606     ldlinux_seg = (size_t) __payload_sseg + data_segment();
607
608     dprintf("argv = %p\n", argv);
609     for (i = 0; i <= argc; i++)
610         dprintf("argv[%d] = %p = \"%s\"\n", i, argv[i], argv[i]);
611
612     (void)argc;                 /* Unused */
613
614     get_dos_version();
615
616     for (argp = argv + 1; *argp; argp++) {
617         if (**argp == '-') {
618             opt = *argp + 1;
619             if (!*opt)
620                 usage();
621
622             while (*opt) {
623                 switch (*opt) {
624                 case 's':       /* Use "safe, slow and stupid" code */
625                     stupid = 1;
626                     break;
627                 case 'r':       /* RAID mode */
628                     raid_mode = 1;
629                     break;
630                 case 'f':       /* Force install */
631                     force = 1;
632                     break;
633                 case 'm':       /* Write MBR */
634                     writembr = 1;
635                     break;
636                 case 'a':       /* Set partition active */
637                     set_active = 1;
638                     break;
639                 case 'd':
640                     if (argp[1])
641                         subdir = *++argp;
642                     break;
643                 default:
644                     usage();
645                 }
646                 opt++;
647             }
648         } else {
649             if (bootsecfile)
650                 usage();
651             else if (device)
652                 bootsecfile = *argp;
653             else
654                 device = *argp;
655         }
656     }
657
658     if (!device)
659         usage();
660
661     /*
662      * Create an ADV in memory... this should be smarter.
663      */
664     syslinux_reset_adv(syslinux_adv);
665
666     /*
667      * Figure out which drive we're talking to
668      */
669     dev_fd = (device[0] & ~0x20) - 0x40;
670     if (dev_fd < 1 || dev_fd > 26 || device[1] != ':' || device[2])
671         usage();
672
673     set_lock_device(dev_fd);
674
675     lock_device(2);             /* Make sure we can lock the device */
676     read_device(dev_fd, sectbuf, 1, 0);
677     unlock_device(1);
678
679     /*
680      * Check to see that what we got was indeed an MS-DOS boot sector/superblock
681      */
682     if ((errmsg = syslinux_check_bootsect(sectbuf))) {
683         unlock_device(0);
684         puts(errmsg);
685         putchar('\n');
686         exit(1);
687     }
688
689     ldlinux_name[0] = dev_fd | 0x40;
690
691     set_attributes(ldlinux_name, 0);
692     fd = creat(ldlinux_name, 0);        /* SYSTEM HIDDEN READONLY */
693     write_ldlinux(fd);
694     write_file(fd, syslinux_adv, 2 * ADV_SIZE);
695     close(fd);
696     set_attributes(ldlinux_name, 0x07); /* SYSTEM HIDDEN READONLY */
697
698     /*
699      * Now, use libfat to create a block map.  This probably
700      * should be changed to use ioctl(...,FIBMAP,...) since
701      * this is supposed to be a simple, privileged version
702      * of the installer.
703      */
704     ldlinux_sectors = (syslinux_ldlinux_len + SECTOR_SIZE - 1) >> SECTOR_SHIFT;
705     sectors = calloc(ldlinux_sectors, sizeof *sectors);
706     lock_device(2);
707     fs = libfat_open(libfat_xpread, dev_fd);
708     ldlinux_cluster = libfat_searchdir(fs, 0, "LDLINUX SYS", NULL);
709     secp = sectors;
710     nsectors = 0;
711     s = libfat_clustertosector(fs, ldlinux_cluster);
712     while (s && nsectors < ldlinux_sectors) {
713         *secp++ = s;
714         nsectors++;
715         s = libfat_nextsector(fs, s);
716     }
717     libfat_close(fs);
718
719     /*
720      * If requested, move ldlinux.sys
721      */
722     if (subdir) {
723         char new_ldlinux_name[160];
724         char *cp = new_ldlinux_name + 3;
725         const char *sd;
726         int slash = 1;
727
728         new_ldlinux_name[0] = dev_fd | 0x40;
729         new_ldlinux_name[1] = ':';
730         new_ldlinux_name[2] = '\\';
731
732         for (sd = subdir; *sd; sd++) {
733             char c = *sd;
734
735             if (c == '/' || c == '\\') {
736                 if (slash)
737                     continue;
738                 c = '\\';
739                 slash = 1;
740             } else {
741                 slash = 0;
742             }
743
744             *cp++ = c;
745         }
746
747         /* Skip if subdirectory == root */
748         if (cp > new_ldlinux_name + 3) {
749             if (!slash)
750                 *cp++ = '\\';
751
752             memcpy(cp, "ldlinux.sys", 12);
753
754             set_attributes(ldlinux_name, 0);
755             if (rename(ldlinux_name, new_ldlinux_name))
756                 set_attributes(ldlinux_name, 0x07);
757             else
758                 set_attributes(new_ldlinux_name, 0x07);
759         }
760     }
761
762     /*
763      * Patch ldlinux.sys and the boot sector
764      */
765     i = syslinux_patch(sectors, nsectors, stupid, raid_mode, subdir, NULL);
766     patch_sectors = (i + SECTOR_SIZE - 1) >> SECTOR_SHIFT;
767
768     /*
769      * Overwrite the now-patched ldlinux.sys
770      */
771     /* lock_device(3); -- doesn't seem to be needed */
772     for (i = 0; i < patch_sectors; i++) {
773         uint16_t si, di, cx;
774         si = 0;
775         di = (size_t) sectbuf;
776         cx = SECTOR_SIZE >> 2;
777         asm volatile ("movw %3,%%fs ; fs ; rep ; movsl":"+S" (si), "+D"(di),
778                       "+c"(cx)
779                       :"abd"((uint16_t)
780                              (ldlinux_seg + (i << (SECTOR_SHIFT - 4)))));
781         write_device(dev_fd, sectbuf, 1, sectors[i]);
782     }
783
784     /*
785      * Muck with the MBR, if desired, while we hold the lock
786      */
787     adjust_mbr(dev_fd, writembr, set_active);
788
789     /*
790      * To finish up, write the boot sector
791      */
792
793     /* Read the superblock again since it might have changed while mounted */
794     read_device(dev_fd, sectbuf, 1, 0);
795
796     /* Copy the syslinux code into the boot sector */
797     syslinux_make_bootsect(sectbuf);
798
799     /* Write new boot sector */
800     if (bootsecfile) {
801         unlock_device(0);
802         fd = creat(bootsecfile, 0x20);  /* ARCHIVE */
803         write_file(fd, sectbuf, SECTOR_SIZE);
804         close(fd);
805     } else {
806         write_device(dev_fd, sectbuf, 1, 0);
807         unlock_device(0);
808     }
809
810     /* Done! */
811
812     return 0;
813 }