1 /* ----------------------------------------------------------------------- *
3 * Copyright 1998-2008 H. Peter Anvin - All Rights Reserved
4 * Copyright 2009-2010 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
35 char *program = "syslinux.com"; /* Name of program */
39 # define dprintf printf
44 asm volatile("int $0x16" : "=a" (ax) : "a" (0));
47 # define dprintf(...) ((void)0)
48 # define pause() ((void)0)
51 void unlock_device(int);
53 void __attribute__ ((noreturn)) die(const char *msg)
62 void warning(const char *msg)
64 puts("syslinux: warning: ");
70 * read/write wrapper functions
72 int creat(const char *filename, int mode)
77 dprintf("creat(\"%s\", 0x%x)\n", filename, mode);
80 asm volatile ("int $0x21 ; setc %0"
81 : "=bcdm" (err), "+a" (rv)
82 : "c" (mode), "d" (filename));
84 dprintf("rv = %d\n", rv);
85 die("cannot open ldlinux.sys");
95 dprintf("close(%d)\n", fd);
97 asm volatile ("int $0x21":"+a" (rv)
100 /* The only error MS-DOS returns for close is EBADF,
101 and we really don't care... */
104 int rename(const char *oldname, const char *newname)
106 uint16_t rv = 0x5600; /* Also support 43FFh? */
109 dprintf("rename(\"%s\", \"%s\")\n", oldname, newname);
111 asm volatile ("int $0x21 ; setc %0":"=bcdm" (err), "+a"(rv)
112 :"d"(oldname), "D"(newname));
115 dprintf("rv = %d\n", rv);
116 warning("cannot move ldlinux.sys");
123 ssize_t write_ldlinux(int fd)
125 uint16_t ldlinux_seg = ((size_t)syslinux_ldlinux >> 4) + ds();
130 while (offset < syslinux_ldlinux_len) {
131 uint32_t chunk = syslinux_ldlinux_len - offset;
134 asm volatile ("pushw %%ds ; "
137 "popw %%ds ; " "setc %0":"=bcdm" (err), "=a"(rv)
138 :"a"(0x4000), "b"(fd), "c"(chunk), "d" (offset & 15),
139 "SD" ((uint16_t)(ldlinux_seg + (offset >> 4))));
141 die("file write error");
148 ssize_t write_file(int fd, const void *buf, size_t count)
154 dprintf("write_file(%d,%p,%u)\n", fd, buf, count);
157 asm volatile ("int $0x21 ; setc %0":"=bcdm" (err), "=a"(rv)
158 :"a"(0x4000), "b"(fd), "c"(count), "d"(buf));
160 die("file write error");
169 static inline __attribute__ ((const))
170 uint16_t data_segment(void)
174 asm("movw %%ds,%0" : "=rm"(ds));
178 void write_device(int drive, const void *buf, size_t nsecs, unsigned int sector)
180 uint16_t errnum = 0x0001;
183 dprintf("write_device(%d,%p,%u,%u)\n", drive, buf, nsecs, sector);
185 dio.startsector = sector;
187 dio.bufoffs = (uintptr_t) buf;
188 dio.bufseg = data_segment();
190 if (dos_version >= 0x070a) {
191 /* Try FAT32-aware system call first */
192 asm volatile("int $0x21 ; jc 1f ; xorw %0,%0\n"
195 : "a" (0x7305), "b" (&dio), "c" (-1), "d" (drive),
198 dprintf(" rv(7305) = %04x", errnum);
201 /* If not supported, try the legacy system call (int2526.S) */
202 if (errnum == 0x0001)
203 errnum = int26_write_sector(drive, &dio);
206 dprintf("rv = %04x\n", errnum);
207 die("sector write error");
211 void read_device(int drive, void *buf, size_t nsecs, unsigned int sector)
213 uint16_t errnum = 0x0001;
216 dprintf("read_device(%d,%p,%u,%u)\n", drive, buf, nsecs, sector);
218 dio.startsector = sector;
220 dio.bufoffs = (uintptr_t) buf;
221 dio.bufseg = data_segment();
223 if (dos_version >= 0x070a) {
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),
230 dprintf(" rv(7305) = %04x", errnum);
233 /* If not supported, try the legacy system call (int2526.S) */
234 if (errnum == 0x0001)
235 errnum = int25_read_sector(drive, &dio);
238 dprintf("rv = %04x\n", errnum);
239 die("sector read error");
243 /* Both traditional DOS and FAT32 DOS return this structure, but
244 FAT32 return a lot more data, so make sure we have plenty of space */
245 struct deviceparams {
251 uint16_t bytespersec;
255 uint16_t rootdirents;
259 uint16_t secpertrack;
262 uint32_t hugesectors;
263 uint8_t lotsofpadding[224];
264 } __attribute__ ((packed));
266 uint32_t get_partition_offset(int drive)
270 struct deviceparams dp;
272 dp.specfunc = 1; /* Get current information */
275 asm volatile ("int $0x21 ; setc %0"
276 :"=abcdm" (err), "+a"(rv), "=m"(dp)
277 :"b" (drive), "c" (0x0860), "d" (&dp));
280 return dp.hiddensecs;
283 asm volatile ("int $0x21 ; setc %0"
284 : "=abcdm" (err), "+a" (rv), "=m" (dp)
285 : "b" (drive), "c" (0x4860), "d" (&dp));
288 return dp.hiddensecs;
290 die("could not find partition start offset");
297 uint16_t firstsector;
299 uint16_t bufferoffset;
301 } __attribute__ ((packed));
303 static struct rwblock mbr = {
307 .firstsector = 0, /* MS-DOS, unlike the BIOS, zero-base sectors */
313 void write_mbr(int drive, const void *buf)
318 dprintf("write_mbr(%d,%p)", drive, buf);
320 mbr.bufferoffset = (uintptr_t) buf;
321 mbr.bufferseg = data_segment();
324 asm volatile ("int $0x21 ; setc %0" : "=bcdm" (err), "+a"(rv)
325 :"c"(0x0841), "d"(&mbr), "b"(drive), "m"(mbr));
327 dprintf(" rv(0841) = %04x", rv);
334 asm volatile ("int $0x21 ; setc %0" : "=bcdm" (err), "+a"(rv)
335 :"c"(0x4841), "d"(&mbr), "b"(drive), "m"(mbr));
337 dprintf(" rv(4841) = %04x\n", rv);
339 die("mbr write error");
342 void read_mbr(int drive, const void *buf)
347 dprintf("read_mbr(%d,%p)", drive, buf);
349 mbr.bufferoffset = (uintptr_t) buf;
350 mbr.bufferseg = data_segment();
353 asm volatile ("int $0x21 ; setc %0":"=abcdm" (err), "+a"(rv)
354 :"c"(0x0861), "d"(&mbr), "b"(drive), "m"(mbr));
356 dprintf(" rv(0861) = %04x", rv);
363 asm volatile ("int $0x21 ; setc %0":"=abcdm" (err), "+a"(rv)
364 :"c"(0x4861), "d"(&mbr), "b"(drive), "m"(mbr));
366 dprintf(" rv(4841) = %04x\n", rv);
368 die("mbr read error");
370 dprintf("Bytes: %02x %02x %02x %02x %02x %02x %02x %02x\n",
371 ((const uint8_t *)buf)[0],
372 ((const uint8_t *)buf)[1],
373 ((const uint8_t *)buf)[2],
374 ((const uint8_t *)buf)[3],
375 ((const uint8_t *)buf)[4],
376 ((const uint8_t *)buf)[5],
377 ((const uint8_t *)buf)[6],
378 ((const uint8_t *)buf)[7]);
381 /* This call can legitimately fail, and we don't care, so ignore error return */
382 void set_attributes(const char *file, int attributes)
384 uint16_t rv = 0x4301;
386 dprintf("set_attributes(\"%s\", 0x%02x)\n", file, attributes);
388 asm volatile ("int $0x21":"+a" (rv)
389 :"c"(attributes), "d"(file));
393 * Version of the read_device function suitable for libfat
395 int libfat_xpread(intptr_t pp, void *buf, size_t secsize,
396 libfat_sector_t sector)
398 read_device(pp, buf, 1, sector);
402 static inline void get_dos_version(void)
406 asm("int $0x21 ; xchgb %%ah,%%al"
412 dprintf("DOS version %d.%d\n", (dos_version >> 8), dos_version & 0xff);
415 /* The locking interface relies on static variables. A massive hack :( */
416 static uint8_t lock_level, lock_drive;
418 static inline void set_lock_device(uint8_t device)
424 static int do_lock(uint8_t level)
426 uint16_t level_arg = lock_drive + (level << 8);
430 /* DOS 7.10 = Win95 OSR2 = first version with FAT32 */
431 uint16_t lock_call = (dos_version >= 0x070a) ? 0x484A : 0x084A;
433 uint16_t lock_call = 0x084A; /* MSDN says this is OK for all filesystems */
436 dprintf("Trying lock %04x... ", level_arg);
437 asm volatile ("int $0x21 ; setc %0"
438 : "=bcdm" (err), "=a" (rv)
439 : "a" (0x440d), "b" (level_arg),
440 "c" (lock_call), "d" (0x0001));
441 dprintf("%s %04x\n", err ? "err" : "ok", rv);
446 void lock_device(int level)
448 static int hard_lock = 0;
451 if (dos_version < 0x0700)
452 return; /* Win9x/NT only */
455 /* Assume hierarchial "soft" locking supported */
457 while (lock_level < level) {
458 int new_level = lock_level + 1;
459 err = do_lock(new_level);
462 /* Try hard locking next */
468 lock_level = new_level;
475 /* Hard locking, only level 4 supported */
476 /* This is needed for Win9x in DOS mode */
481 /* Assume locking is not needed */
492 die("could not lock device");
495 void unlock_device(int level)
499 uint16_t unlock_call;
501 if (dos_version < 0x0700)
502 return; /* Win9x/NT only */
505 /* DOS 7.10 = Win95 OSR2 = first version with FAT32 */
506 unlock_call = (dos_version >= 0x070a) ? 0x486A : 0x086A;
508 unlock_call = 0x086A; /* MSDN says this is OK for all filesystems */
511 if (lock_level == 4 && level > 0)
512 return; /* Only drop the hard lock at the end */
514 while (lock_level > level) {
515 uint8_t new_level = (lock_level == 4) ? 0 : lock_level - 1;
516 uint16_t level_arg = (new_level << 8) + lock_drive;
518 dprintf("Trying unlock %04x... ", new_level);
519 asm volatile ("int $0x21 ; setc %0"
520 : "=bcdm" (err), "+a" (rv)
521 : "b" (level_arg), "c" (unlock_call));
522 dprintf("%s %04x\n", err ? "err" : "ok", rv);
523 lock_level = new_level;
528 * This function does any desired MBR manipulation; called with the device lock held.
531 uint8_t active; /* Active flag */
532 uint8_t bhead; /* Begin head */
533 uint8_t bsector; /* Begin sector */
534 uint8_t bcylinder; /* Begin cylinder */
535 uint8_t filesystem; /* Filesystem value */
536 uint8_t ehead; /* End head */
537 uint8_t esector; /* End sector */
538 uint8_t ecylinder; /* End cylinder */
539 uint32_t startlba; /* Start sector LBA */
540 uint32_t sectors; /* Length in sectors */
541 } __attribute__ ((packed));
543 static void adjust_mbr(int device, int writembr, int set_active)
545 static unsigned char sectbuf[SECTOR_SIZE];
548 if (!writembr && !set_active)
549 return; /* Nothing to do */
551 read_mbr(device, sectbuf);
554 memcpy(sectbuf, syslinux_mbr, syslinux_mbr_len);
555 *(uint16_t *) (sectbuf + 510) = 0xaa55;
559 uint32_t offset = get_partition_offset(device);
560 struct mbr_entry *me = (struct mbr_entry *)(sectbuf + 446);
563 dprintf("Searching for partition offset: %08x\n", offset);
565 for (i = 0; i < 4; i++) {
566 if (me->startlba == offset) {
576 die("partition not found (-a is not implemented for logical partitions)");
577 } else if (found > 1) {
578 die("multiple aliased partitions found");
582 write_mbr(device, sectbuf);
585 int main(int argc, char *argv[])
587 static unsigned char sectbuf[SECTOR_SIZE];
589 static char ldlinux_name[] = "@:\\ldlinux.sys";
590 struct libfat_filesystem *fs;
591 libfat_sector_t s, *secp;
592 libfat_sector_t *sectors;
594 int32_t ldlinux_cluster;
601 dprintf("argv = %p\n", argv);
602 for (i = 0; i <= argc; i++)
603 dprintf("argv[%d] = %p = \"%s\"\n", i, argv[i], argv[i]);
608 parse_options(argc, argv, MODE_SYSLINUX_DOSWIN);
611 usage(EX_USAGE, MODE_SYSLINUX_DOSWIN);
612 if (opt.sectors || opt.heads || opt.reset_adv || opt.set_once
613 || (opt.update_only > 0) || opt.menu_save || opt.offset) {
615 "At least one specified option not yet implemented"
616 " for this installer.\n");
621 * Create an ADV in memory... this should be smarter.
623 syslinux_reset_adv(syslinux_adv);
626 * Figure out which drive we're talking to
628 dev_fd = (opt.device[0] & ~0x20) - 0x40;
629 if (dev_fd < 1 || dev_fd > 26 || opt.device[1] != ':' || opt.device[2])
630 usage(EX_USAGE, MODE_SYSLINUX_DOSWIN);
632 set_lock_device(dev_fd);
634 lock_device(2); /* Make sure we can lock the device */
635 read_device(dev_fd, sectbuf, 1, 0);
639 * Check to see that what we got was indeed an MS-DOS boot sector/superblock
641 if ((errmsg = syslinux_check_bootsect(sectbuf))) {
648 ldlinux_name[0] = dev_fd | 0x40;
650 set_attributes(ldlinux_name, 0);
651 fd = creat(ldlinux_name, 0); /* SYSTEM HIDDEN READONLY */
653 write_file(fd, syslinux_adv, 2 * ADV_SIZE);
655 set_attributes(ldlinux_name, 0x07); /* SYSTEM HIDDEN READONLY */
658 * Now, use libfat to create a block map. This probably
659 * should be changed to use ioctl(...,FIBMAP,...) since
660 * this is supposed to be a simple, privileged version
663 ldlinux_sectors = (syslinux_ldlinux_len + 2 * ADV_SIZE
664 + SECTOR_SIZE - 1) >> SECTOR_SHIFT;
665 sectors = calloc(ldlinux_sectors, sizeof *sectors);
667 fs = libfat_open(libfat_xpread, dev_fd);
668 ldlinux_cluster = libfat_searchdir(fs, 0, "LDLINUX SYS", NULL);
671 s = libfat_clustertosector(fs, ldlinux_cluster);
672 while (s && nsectors < ldlinux_sectors) {
675 s = libfat_nextsector(fs, s);
680 * If requested, move ldlinux.sys
683 char new_ldlinux_name[160];
684 char *cp = new_ldlinux_name + 3;
688 new_ldlinux_name[0] = dev_fd | 0x40;
689 new_ldlinux_name[1] = ':';
690 new_ldlinux_name[2] = '\\';
692 for (sd = opt.directory; *sd; sd++) {
695 if (c == '/' || c == '\\') {
707 /* Skip if subdirectory == root */
708 if (cp > new_ldlinux_name + 3) {
712 memcpy(cp, "ldlinux.sys", 12);
714 set_attributes(ldlinux_name, 0);
715 if (rename(ldlinux_name, new_ldlinux_name))
716 set_attributes(ldlinux_name, 0x07);
718 set_attributes(new_ldlinux_name, 0x07);
723 * Patch ldlinux.sys and the boot sector
725 i = syslinux_patch(sectors, nsectors, opt.stupid_mode, opt.raid_mode, opt.directory, NULL);
726 patch_sectors = (i + SECTOR_SIZE - 1) >> SECTOR_SHIFT;
729 * Overwrite the now-patched ldlinux.sys
731 /* lock_device(3); -- doesn't seem to be needed */
732 dp = syslinux_ldlinux;
733 for (i = 0; i < patch_sectors; i++) {
734 memcpy_from_sl(sectbuf, dp, SECTOR_SIZE);
736 write_device(dev_fd, sectbuf, 1, sectors[i]);
740 * Muck with the MBR, if desired, while we hold the lock
742 adjust_mbr(dev_fd, opt.install_mbr, opt.activate_partition);
745 * To finish up, write the boot sector
748 /* Read the superblock again since it might have changed while mounted */
749 read_device(dev_fd, sectbuf, 1, 0);
751 /* Copy the syslinux code into the boot sector */
752 syslinux_make_bootsect(sectbuf);
754 /* Write new boot sector */
755 if (opt.bootsecfile) {
757 fd = creat(opt.bootsecfile, 0x20); /* ARCHIVE */
758 write_file(fd, sectbuf, SECTOR_SIZE);
761 write_device(dev_fd, sectbuf, 1, 0);