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