1 /* ----------------------------------------------------------------------- *
3 * Copyright 1998-2008 H. Peter Anvin - All Rights Reserved
4 * Copyright 2009 Intel Corporation; author: H. Peter Anvin
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.
12 * ----------------------------------------------------------------------- */
15 * syslinux.c - Linux installer program for SYSLINUX
31 const char *program = "syslinux"; /* Name of program */
35 # define dprintf printf
40 asm volatile("int $0x16" : "=a" (ax) : "a" (0));
43 # define dprintf(...) ((void)0)
44 # define pause() ((void)0)
47 void __attribute__ ((noreturn)) usage(void)
49 puts("Usage: syslinux [-sfmar][-d directory] <drive>: [bootsecfile]\n");
53 void unlock_device(int);
55 void __attribute__ ((noreturn)) die(const char *msg)
64 void warning(const char *msg)
66 puts("syslinux: warning: ");
72 * read/write wrapper functions
74 int creat(const char *filename, int mode)
79 dprintf("creat(\"%s\", 0x%x)\n", filename, mode);
82 asm volatile ("int $0x21 ; setc %0"
83 : "=bcdm" (err), "+a" (rv)
84 : "c" (mode), "d" (filename));
86 dprintf("rv = %d\n", rv);
87 die("cannot open ldlinux.sys");
97 dprintf("close(%d)\n", fd);
99 asm volatile ("int $0x21":"+a" (rv)
102 /* The only error MS-DOS returns for close is EBADF,
103 and we really don't care... */
106 int rename(const char *oldname, const char *newname)
108 uint16_t rv = 0x5600; /* Also support 43FFh? */
111 dprintf("rename(\"%s\", \"%s\")\n", oldname, newname);
113 asm volatile ("int $0x21 ; setc %0":"=bcdm" (err), "+a"(rv)
114 :"d"(oldname), "D"(newname));
117 dprintf("rv = %d\n", rv);
118 warning("cannot move ldlinux.sys");
125 extern const char __payload_sseg[];
126 uint16_t ldlinux_seg;
128 ssize_t write_ldlinux(int fd)
134 while (offset < syslinux_ldlinux_len) {
135 uint32_t chunk = syslinux_ldlinux_len - offset;
138 asm volatile ("pushw %%ds ; "
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))));
145 die("file write error");
152 ssize_t write_file(int fd, const void *buf, size_t count)
158 dprintf("write_file(%d,%p,%u)\n", fd, buf, count);
161 asm volatile ("int $0x21 ; setc %0":"=bcdm" (err), "=a"(rv)
162 :"a"(0x4000), "b"(fd), "c"(count), "d"(buf));
164 die("file write error");
173 static inline __attribute__ ((const))
174 uint16_t data_segment(void)
178 asm("movw %%ds,%0" : "=rm"(ds));
182 void write_device(int drive, const void *buf, size_t nsecs, unsigned int sector)
187 dprintf("write_device(%d,%p,%u,%u)\n", drive, buf, nsecs, sector);
189 dio.startsector = sector;
191 dio.bufoffs = (uintptr_t) buf;
192 dio.bufseg = data_segment();
194 /* Try FAT32-aware system call first */
195 asm volatile("int $0x21 ; jc 1f ; xorw %0,%0\n"
198 : "a" (0x7305), "b" (&dio), "c" (-1), "d" (drive),
202 /* If not supported, try the legacy system call (int2526.S) */
203 if (errnum == 0x0001)
204 errnum = int26_write_sector(drive, &dio);
207 dprintf("rv = %04x\n", errnum);
208 die("sector write error");
212 void read_device(int drive, const void *buf, size_t nsecs, unsigned int sector)
217 dprintf("read_device(%d,%p,%u,%u)\n", drive, buf, nsecs, sector);
219 dio.startsector = sector;
221 dio.bufoffs = (uintptr_t) buf;
222 dio.bufseg = data_segment();
224 /* Try FAT32-aware system call first */
225 asm volatile("int $0x21 ; jc 1f ; xorw %0,%0\n"
228 : "a" (0x7305), "b" (&dio), "c" (-1), "d" (drive),
231 /* If not supported, try the legacy system call (int2526.S) */
232 if (errnum == 0x0001)
233 errnum = int25_read_sector(drive, &dio);
236 dprintf("rv = %04x\n", errnum);
237 die("sector read error");
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 {
249 uint16_t bytespersec;
253 uint16_t rootdirents;
257 uint16_t secpertrack;
260 uint32_t hugesectors;
261 uint8_t lotsofpadding[224];
262 } __attribute__ ((packed));
264 uint32_t get_partition_offset(int drive)
268 struct deviceparams dp;
270 dp.specfunc = 1; /* Get current information */
273 asm volatile ("int $0x21 ; setc %0"
274 :"=abcdm" (err), "+a"(rv), "=m"(dp)
275 :"b" (drive), "c" (0x0860), "d" (&dp));
278 return dp.hiddensecs;
281 asm volatile ("int $0x21 ; setc %0"
282 : "=abcdm" (err), "+a" (rv), "=m" (dp)
283 : "b" (drive), "c" (0x4860), "d" (&dp));
286 return dp.hiddensecs;
288 die("could not find partition start offset");
295 uint16_t firstsector;
297 uint16_t bufferoffset;
299 } __attribute__ ((packed));
301 static struct rwblock mbr = {
305 .firstsector = 0, /* MS-DOS, unlike the BIOS, zero-base sectors */
311 void write_mbr(int drive, const void *buf)
316 dprintf("write_mbr(%d,%p)", drive, buf);
318 mbr.bufferoffset = (uintptr_t) buf;
319 mbr.bufferseg = data_segment();
322 asm volatile ("int $0x21 ; setc %0" : "=bcdm" (err), "+a"(rv)
323 :"c"(0x0841), "d"(&mbr), "b"(drive), "m"(mbr));
325 dprintf(" rv(0841) = %04x", rv);
332 asm volatile ("int $0x21 ; setc %0" : "=bcdm" (err), "+a"(rv)
333 :"c"(0x4841), "d"(&mbr), "b"(drive), "m"(mbr));
335 dprintf(" rv(4841) = %04x\n", rv);
337 die("mbr write error");
340 void read_mbr(int drive, const void *buf)
345 dprintf("read_mbr(%d,%p)", drive, buf);
347 mbr.bufferoffset = (uintptr_t) buf;
348 mbr.bufferseg = data_segment();
351 asm volatile ("int $0x21 ; setc %0":"=abcdm" (err), "+a"(rv)
352 :"c"(0x0861), "d"(&mbr), "b"(drive), "m"(mbr));
354 dprintf(" rv(0861) = %04x", rv);
361 asm volatile ("int $0x21 ; setc %0":"=abcdm" (err), "+a"(rv)
362 :"c"(0x4861), "d"(&mbr), "b"(drive), "m"(mbr));
364 dprintf(" rv(4841) = %04x\n", rv);
366 die("mbr read error");
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]);
379 /* This call can legitimately fail, and we don't care, so ignore error return */
380 void set_attributes(const char *file, int attributes)
382 uint16_t rv = 0x4301;
384 dprintf("set_attributes(\"%s\", 0x%02x)\n", file, attributes);
386 asm volatile ("int $0x21":"+a" (rv)
387 :"c"(attributes), "d"(file));
391 * Version of the read_device function suitable for libfat
393 int libfat_xpread(intptr_t pp, void *buf, size_t secsize,
394 libfat_sector_t sector)
396 read_device(pp, buf, 1, sector);
400 static inline void get_dos_version(void)
404 asm("int $0x21 ; xchgb %%ah,%%al"
410 dprintf("DOS version %d.%d\n", (dos_version >> 8), dos_version & 0xff);
413 /* The locking interface relies on static variables. A massive hack :( */
414 static uint8_t lock_level, lock_drive;
416 static inline void set_lock_device(uint8_t device)
422 static int do_lock(uint8_t level)
424 uint16_t level_arg = lock_drive + (level << 8);
428 /* DOS 7.10 = Win95 OSR2 = first version with FAT32 */
429 uint16_t lock_call = (dos_version >= 0x070a) ? 0x484A : 0x084A;
431 uint16_t lock_call = 0x084A; /* MSDN says this is OK for all filesystems */
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);
444 void lock_device(int level)
446 static int hard_lock = 0;
449 if (dos_version < 0x0700)
450 return; /* Win9x/NT only */
453 /* Assume hierarchial "soft" locking supported */
455 while (lock_level < level) {
456 int new_level = lock_level + 1;
457 err = do_lock(new_level);
460 /* Try hard locking next */
466 lock_level = new_level;
473 /* Hard locking, only level 4 supported */
474 /* This is needed for Win9x in DOS mode */
479 /* Assume locking is not needed */
490 die("could not lock device");
493 void unlock_device(int level)
497 uint16_t unlock_call;
499 if (dos_version < 0x0700)
500 return; /* Win9x/NT only */
503 /* DOS 7.10 = Win95 OSR2 = first version with FAT32 */
504 unlock_call = (dos_version >= 0x070a) ? 0x486A : 0x086A;
506 unlock_call = 0x086A; /* MSDN says this is OK for all filesystems */
509 if (lock_level == 4 && level > 0)
510 return; /* Only drop the hard lock at the end */
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;
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;
526 * This function does any desired MBR manipulation; called with the device lock held.
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));
541 static void adjust_mbr(int device, int writembr, int set_active)
543 static unsigned char sectbuf[SECTOR_SIZE];
546 if (!writembr && !set_active)
547 return; /* Nothing to do */
549 read_mbr(device, sectbuf);
552 memcpy(sectbuf, syslinux_mbr, syslinux_mbr_len);
553 *(uint16_t *) (sectbuf + 510) = 0xaa55;
557 uint32_t offset = get_partition_offset(device);
558 struct mbr_entry *me = (struct mbr_entry *)(sectbuf + 446);
561 dprintf("Searching for partition offset: %08x\n", offset);
563 for (i = 0; i < 4; i++) {
564 if (me->startlba == offset) {
574 die("partition not found (-a is not implemented for logical partitions)");
575 } else if (found > 1) {
576 die("multiple aliased partitions found");
580 write_mbr(device, sectbuf);
583 int main(int argc, char *argv[])
585 static unsigned char sectbuf[SECTOR_SIZE];
587 static char ldlinux_name[] = "@:\\ldlinux.sys";
589 int force = 0; /* -f (force) option */
590 struct libfat_filesystem *fs;
591 libfat_sector_t s, *secp;
592 libfat_sector_t *sectors;
594 int32_t ldlinux_cluster;
596 const char *device = NULL, *bootsecfile = NULL;
599 int writembr = 0; /* -m (write MBR) option */
600 int set_active = 0; /* -a (set partition active) option */
601 const char *subdir = NULL;
606 ldlinux_seg = (size_t) __payload_sseg + data_segment();
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]);
612 (void)argc; /* Unused */
616 for (argp = argv + 1; *argp; argp++) {
624 case 's': /* Use "safe, slow and stupid" code */
627 case 'r': /* RAID mode */
630 case 'f': /* Force install */
633 case 'm': /* Write MBR */
636 case 'a': /* Set partition active */
662 * Create an ADV in memory... this should be smarter.
664 syslinux_reset_adv(syslinux_adv);
667 * Figure out which drive we're talking to
669 dev_fd = (device[0] & ~0x20) - 0x40;
670 if (dev_fd < 1 || dev_fd > 26 || device[1] != ':' || device[2])
673 set_lock_device(dev_fd);
675 lock_device(2); /* Make sure we can lock the device */
676 read_device(dev_fd, sectbuf, 1, 0);
680 * Check to see that what we got was indeed an MS-DOS boot sector/superblock
682 if ((errmsg = syslinux_check_bootsect(sectbuf))) {
689 ldlinux_name[0] = dev_fd | 0x40;
691 set_attributes(ldlinux_name, 0);
692 fd = creat(ldlinux_name, 0); /* SYSTEM HIDDEN READONLY */
694 write_file(fd, syslinux_adv, 2 * ADV_SIZE);
696 set_attributes(ldlinux_name, 0x07); /* SYSTEM HIDDEN READONLY */
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
704 ldlinux_sectors = (syslinux_ldlinux_len + SECTOR_SIZE - 1) >> SECTOR_SHIFT;
705 sectors = calloc(ldlinux_sectors, sizeof *sectors);
707 fs = libfat_open(libfat_xpread, dev_fd);
708 ldlinux_cluster = libfat_searchdir(fs, 0, "LDLINUX SYS", NULL);
711 s = libfat_clustertosector(fs, ldlinux_cluster);
712 while (s && nsectors < ldlinux_sectors) {
715 s = libfat_nextsector(fs, s);
720 * If requested, move ldlinux.sys
723 char new_ldlinux_name[160];
724 char *cp = new_ldlinux_name + 3;
728 new_ldlinux_name[0] = dev_fd | 0x40;
729 new_ldlinux_name[1] = ':';
730 new_ldlinux_name[2] = '\\';
732 for (sd = subdir; *sd; sd++) {
735 if (c == '/' || c == '\\') {
747 /* Skip if subdirectory == root */
748 if (cp > new_ldlinux_name + 3) {
752 memcpy(cp, "ldlinux.sys", 12);
754 set_attributes(ldlinux_name, 0);
755 if (rename(ldlinux_name, new_ldlinux_name))
756 set_attributes(ldlinux_name, 0x07);
758 set_attributes(new_ldlinux_name, 0x07);
763 * Patch ldlinux.sys and the boot sector
765 i = syslinux_patch(sectors, nsectors, stupid, raid_mode, subdir, NULL);
766 patch_sectors = (i + SECTOR_SIZE - 1) >> SECTOR_SHIFT;
769 * Overwrite the now-patched ldlinux.sys
771 /* lock_device(3); -- doesn't seem to be needed */
772 for (i = 0; i < patch_sectors; i++) {
775 di = (size_t) sectbuf;
776 cx = SECTOR_SIZE >> 2;
777 asm volatile ("movw %3,%%fs ; fs ; rep ; movsl":"+S" (si), "+D"(di),
780 (ldlinux_seg + (i << (SECTOR_SHIFT - 4)))));
781 write_device(dev_fd, sectbuf, 1, sectors[i]);
785 * Muck with the MBR, if desired, while we hold the lock
787 adjust_mbr(dev_fd, writembr, set_active);
790 * To finish up, write the boot sector
793 /* Read the superblock again since it might have changed while mounted */
794 read_device(dev_fd, sectbuf, 1, 0);
796 /* Copy the syslinux code into the boot sector */
797 syslinux_make_bootsect(sectbuf);
799 /* Write new boot sector */
802 fd = creat(bootsecfile, 0x20); /* ARCHIVE */
803 write_file(fd, sectbuf, SECTOR_SIZE);
806 write_device(dev_fd, sectbuf, 1, 0);