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