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
36 char *program
= "syslinux.com"; /* Name of program */
40 # define dprintf printf
45 asm volatile("int $0x16" : "=a" (ax
) : "a" (0));
48 # define dprintf(...) ((void)0)
49 # define pause() ((void)0)
52 void unlock_device(int);
54 void __attribute__ ((noreturn
)) die(const char *msg
)
63 void warning(const char *msg
)
65 puts("syslinux: warning: ");
71 * read/write wrapper functions
73 int creat(const char *filename
, int mode
)
78 dprintf("creat(\"%s\", 0x%x)\n", filename
, mode
);
81 asm volatile ("int $0x21 ; setc %0"
82 : "=bcdm" (err
), "+a" (rv
)
83 : "c" (mode
), "d" (filename
));
85 dprintf("rv = %d\n", rv
);
86 die("cannot open ldlinux.sys");
96 dprintf("close(%d)\n", fd
);
98 asm volatile ("int $0x21":"+a" (rv
)
101 /* The only error MS-DOS returns for close is EBADF,
102 and we really don't care... */
105 int rename(const char *oldname
, const char *newname
)
107 uint16_t rv
= 0x5600; /* Also support 43FFh? */
110 dprintf("rename(\"%s\", \"%s\")\n", oldname
, newname
);
112 asm volatile ("int $0x21 ; setc %0":"=bcdm" (err
), "+a"(rv
)
113 :"d"(oldname
), "D"(newname
));
116 dprintf("rv = %d\n", rv
);
117 warning("cannot move ldlinux.sys");
124 ssize_t
write_ldlinux(int fd
)
126 uint16_t ldlinux_seg
= ((size_t)syslinux_ldlinux
>> 4) + ds();
131 while (offset
< syslinux_ldlinux_len
) {
132 uint32_t chunk
= syslinux_ldlinux_len
- offset
;
135 asm volatile ("pushw %%ds ; "
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))));
142 die("file write error");
149 ssize_t
write_file(int fd
, const void *buf
, size_t count
)
155 dprintf("write_file(%d,%p,%u)\n", fd
, buf
, count
);
158 asm volatile ("int $0x21 ; setc %0":"=bcdm" (err
), "=a"(rv
)
159 :"a"(0x4000), "b"(fd
), "c"(count
), "d"(buf
));
161 die("file write error");
170 static inline __attribute__ ((const))
171 uint16_t data_segment(void)
175 asm("movw %%ds,%0" : "=rm"(ds
));
179 void write_device(int drive
, const void *buf
, size_t nsecs
, unsigned int sector
)
181 uint16_t errnum
= 0x0001;
184 dprintf("write_device(%d,%p,%u,%u)\n", drive
, buf
, nsecs
, sector
);
186 dio
.startsector
= sector
;
188 dio
.bufoffs
= (uintptr_t) buf
;
189 dio
.bufseg
= data_segment();
191 if (dos_version
>= 0x070a) {
192 /* Try FAT32-aware system call first */
193 asm volatile("int $0x21 ; jc 1f ; xorw %0,%0\n"
196 : "a" (0x7305), "b" (&dio
), "c" (-1), "d" (drive
),
199 dprintf(" rv(7305) = %04x", errnum
);
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
, void *buf
, size_t nsecs
, unsigned int sector
)
214 uint16_t errnum
= 0x0001;
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 if (dos_version
>= 0x070a) {
225 /* Try FAT32-aware system call first */
226 asm volatile("int $0x21 ; jc 1f ; xorw %0,%0\n"
229 : "a" (0x7305), "b" (&dio
), "c" (-1), "d" (drive
),
231 dprintf(" rv(7305) = %04x", errnum
);
234 /* If not supported, try the legacy system call (int2526.S) */
235 if (errnum
== 0x0001)
236 errnum
= int25_read_sector(drive
, &dio
);
239 dprintf("rv = %04x\n", errnum
);
240 die("sector read error");
244 /* Both traditional DOS and FAT32 DOS return this structure, but
245 FAT32 return a lot more data, so make sure we have plenty of space */
246 struct deviceparams
{
252 uint16_t bytespersec
;
256 uint16_t rootdirents
;
260 uint16_t secpertrack
;
263 uint32_t hugesectors
;
264 uint8_t lotsofpadding
[224];
265 } __attribute__ ((packed
));
267 uint32_t get_partition_offset(int drive
)
271 struct deviceparams dp
;
273 dp
.specfunc
= 1; /* Get current information */
276 asm volatile ("int $0x21 ; setc %0"
277 :"=abcdm" (err
), "+a"(rv
), "=m"(dp
)
278 :"b" (drive
), "c" (0x0860), "d" (&dp
));
281 return dp
.hiddensecs
;
284 asm volatile ("int $0x21 ; setc %0"
285 : "=abcdm" (err
), "+a" (rv
), "=m" (dp
)
286 : "b" (drive
), "c" (0x4860), "d" (&dp
));
289 return dp
.hiddensecs
;
291 die("could not find partition start offset");
298 uint16_t firstsector
;
300 uint16_t bufferoffset
;
302 } __attribute__ ((packed
));
304 static struct rwblock mbr
= {
308 .firstsector
= 0, /* MS-DOS, unlike the BIOS, zero-base sectors */
314 void write_mbr(int drive
, const void *buf
)
319 dprintf("write_mbr(%d,%p)", drive
, buf
);
321 mbr
.bufferoffset
= (uintptr_t) buf
;
322 mbr
.bufferseg
= data_segment();
325 asm volatile ("int $0x21 ; setc %0" : "=bcdm" (err
), "+a"(rv
)
326 :"c"(0x0841), "d"(&mbr
), "b"(drive
), "m"(mbr
));
328 dprintf(" rv(0841) = %04x", rv
);
335 asm volatile ("int $0x21 ; setc %0" : "=bcdm" (err
), "+a"(rv
)
336 :"c"(0x4841), "d"(&mbr
), "b"(drive
), "m"(mbr
));
338 dprintf(" rv(4841) = %04x\n", rv
);
340 die("mbr write error");
343 void read_mbr(int drive
, const void *buf
)
348 dprintf("read_mbr(%d,%p)", drive
, buf
);
350 mbr
.bufferoffset
= (uintptr_t) buf
;
351 mbr
.bufferseg
= data_segment();
354 asm volatile ("int $0x21 ; setc %0":"=abcdm" (err
), "+a"(rv
)
355 :"c"(0x0861), "d"(&mbr
), "b"(drive
), "m"(mbr
));
357 dprintf(" rv(0861) = %04x", rv
);
364 asm volatile ("int $0x21 ; setc %0":"=abcdm" (err
), "+a"(rv
)
365 :"c"(0x4861), "d"(&mbr
), "b"(drive
), "m"(mbr
));
367 dprintf(" rv(4841) = %04x\n", rv
);
369 die("mbr read error");
371 dprintf("Bytes: %02x %02x %02x %02x %02x %02x %02x %02x\n",
372 ((const uint8_t *)buf
)[0],
373 ((const uint8_t *)buf
)[1],
374 ((const uint8_t *)buf
)[2],
375 ((const uint8_t *)buf
)[3],
376 ((const uint8_t *)buf
)[4],
377 ((const uint8_t *)buf
)[5],
378 ((const uint8_t *)buf
)[6],
379 ((const uint8_t *)buf
)[7]);
382 /* This call can legitimately fail, and we don't care, so ignore error return */
383 void set_attributes(const char *file
, int attributes
)
385 uint16_t rv
= 0x4301;
387 dprintf("set_attributes(\"%s\", 0x%02x)\n", file
, attributes
);
389 asm volatile ("int $0x21":"+a" (rv
)
390 :"c"(attributes
), "d"(file
));
394 * Version of the read_device function suitable for libfat
396 int libfat_xpread(intptr_t pp
, void *buf
, size_t secsize
,
397 libfat_sector_t sector
)
399 read_device(pp
, buf
, 1, sector
);
403 static inline void get_dos_version(void)
407 asm("int $0x21 ; xchgb %%ah,%%al"
413 dprintf("DOS version %d.%d\n", (dos_version
>> 8), dos_version
& 0xff);
416 /* The locking interface relies on static variables. A massive hack :( */
417 static uint8_t lock_level
, lock_drive
;
419 static inline void set_lock_device(uint8_t device
)
425 static int do_lock(uint8_t level
)
427 uint16_t level_arg
= lock_drive
+ (level
<< 8);
431 /* DOS 7.10 = Win95 OSR2 = first version with FAT32 */
432 uint16_t lock_call
= (dos_version
>= 0x070a) ? 0x484A : 0x084A;
434 uint16_t lock_call
= 0x084A; /* MSDN says this is OK for all filesystems */
437 dprintf("Trying lock %04x... ", level_arg
);
438 asm volatile ("int $0x21 ; setc %0"
439 : "=bcdm" (err
), "=a" (rv
)
440 : "a" (0x440d), "b" (level_arg
),
441 "c" (lock_call
), "d" (0x0001));
442 dprintf("%s %04x\n", err
? "err" : "ok", rv
);
447 void lock_device(int level
)
449 static int hard_lock
= 0;
452 if (dos_version
< 0x0700)
453 return; /* Win9x/NT only */
456 /* Assume hierarchial "soft" locking supported */
458 while (lock_level
< level
) {
459 int new_level
= lock_level
+ 1;
460 err
= do_lock(new_level
);
463 /* Try hard locking next */
469 lock_level
= new_level
;
476 /* Hard locking, only level 4 supported */
477 /* This is needed for Win9x in DOS mode */
482 /* Assume locking is not needed */
493 die("could not lock device");
496 void unlock_device(int level
)
500 uint16_t unlock_call
;
502 if (dos_version
< 0x0700)
503 return; /* Win9x/NT only */
506 /* DOS 7.10 = Win95 OSR2 = first version with FAT32 */
507 unlock_call
= (dos_version
>= 0x070a) ? 0x486A : 0x086A;
509 unlock_call
= 0x086A; /* MSDN says this is OK for all filesystems */
512 if (lock_level
== 4 && level
> 0)
513 return; /* Only drop the hard lock at the end */
515 while (lock_level
> level
) {
516 uint8_t new_level
= (lock_level
== 4) ? 0 : lock_level
- 1;
517 uint16_t level_arg
= (new_level
<< 8) + lock_drive
;
519 dprintf("Trying unlock %04x... ", new_level
);
520 asm volatile ("int $0x21 ; setc %0"
521 : "=bcdm" (err
), "+a" (rv
)
522 : "b" (level_arg
), "c" (unlock_call
));
523 dprintf("%s %04x\n", err
? "err" : "ok", rv
);
524 lock_level
= new_level
;
529 * This function does any desired MBR manipulation; called with the device lock held.
532 uint8_t active
; /* Active flag */
533 uint8_t bhead
; /* Begin head */
534 uint8_t bsector
; /* Begin sector */
535 uint8_t bcylinder
; /* Begin cylinder */
536 uint8_t filesystem
; /* Filesystem value */
537 uint8_t ehead
; /* End head */
538 uint8_t esector
; /* End sector */
539 uint8_t ecylinder
; /* End cylinder */
540 uint32_t startlba
; /* Start sector LBA */
541 uint32_t sectors
; /* Length in sectors */
542 } __attribute__ ((packed
));
544 static void adjust_mbr(int device
, int writembr
, int set_active
)
546 static unsigned char sectbuf
[SECTOR_SIZE
];
549 if (!writembr
&& !set_active
)
550 return; /* Nothing to do */
552 read_mbr(device
, sectbuf
);
555 memcpy(sectbuf
, syslinux_mbr
, syslinux_mbr_len
);
556 *(uint16_t *) (sectbuf
+ 510) = 0xaa55;
560 uint32_t offset
= get_partition_offset(device
);
561 struct mbr_entry
*me
= (struct mbr_entry
*)(sectbuf
+ 446);
564 dprintf("Searching for partition offset: %08x\n", offset
);
566 for (i
= 0; i
< 4; i
++) {
567 if (me
->startlba
== offset
) {
577 die("partition not found (-a is not implemented for logical partitions)");
578 } else if (found
> 1) {
579 die("multiple aliased partitions found");
583 write_mbr(device
, sectbuf
);
586 int main(int argc
, char *argv
[])
588 static unsigned char sectbuf
[SECTOR_SIZE
];
590 static char ldlinux_name
[] = "@:\\ldlinux.sys";
591 struct libfat_filesystem
*fs
;
592 libfat_sector_t s
, *secp
;
593 libfat_sector_t
*sectors
;
595 int32_t ldlinux_cluster
;
602 dprintf("argv = %p\n", argv
);
603 for (i
= 0; i
<= argc
; i
++)
604 dprintf("argv[%d] = %p = \"%s\"\n", i
, argv
[i
], argv
[i
]);
609 parse_options(argc
, argv
, MODE_SYSLINUX_DOSWIN
);
612 usage(EX_USAGE
, MODE_SYSLINUX_DOSWIN
);
613 if (opt
.sectors
|| opt
.heads
|| opt
.reset_adv
|| opt
.set_once
614 || (opt
.update_only
> 0) || opt
.menu_save
|| opt
.offset
) {
616 "At least one specified option not yet implemented"
617 " for this installer.\n");
622 * Create an ADV in memory... this should be smarter.
624 syslinux_reset_adv(syslinux_adv
);
627 * Figure out which drive we're talking to
629 dev_fd
= (opt
.device
[0] & ~0x20) - 0x40;
630 if (dev_fd
< 1 || dev_fd
> 26 || opt
.device
[1] != ':' || opt
.device
[2])
631 usage(EX_USAGE
, MODE_SYSLINUX_DOSWIN
);
633 set_lock_device(dev_fd
);
635 lock_device(2); /* Make sure we can lock the device */
636 read_device(dev_fd
, sectbuf
, 1, 0);
640 * Check to see that what we got was indeed an MS-DOS boot sector/superblock
642 if ((errmsg
= syslinux_check_bootsect(sectbuf
, NULL
))) {
649 ldlinux_name
[0] = dev_fd
| 0x40;
651 set_attributes(ldlinux_name
, 0);
652 fd
= creat(ldlinux_name
, 0); /* SYSTEM HIDDEN READONLY */
654 write_file(fd
, syslinux_adv
, 2 * ADV_SIZE
);
656 set_attributes(ldlinux_name
, 0x07); /* SYSTEM HIDDEN READONLY */
659 * Now, use libfat to create a block map. This probably
660 * should be changed to use ioctl(...,FIBMAP,...) since
661 * this is supposed to be a simple, privileged version
664 ldlinux_sectors
= (syslinux_ldlinux_len
+ 2 * ADV_SIZE
665 + SECTOR_SIZE
- 1) >> SECTOR_SHIFT
;
666 sectors
= calloc(ldlinux_sectors
, sizeof *sectors
);
668 fs
= libfat_open(libfat_xpread
, dev_fd
);
669 ldlinux_cluster
= libfat_searchdir(fs
, 0, "LDLINUX SYS", NULL
);
672 s
= libfat_clustertosector(fs
, ldlinux_cluster
);
673 while (s
&& nsectors
< ldlinux_sectors
) {
676 s
= libfat_nextsector(fs
, s
);
681 * If requested, move ldlinux.sys
684 char new_ldlinux_name
[160];
685 char *cp
= new_ldlinux_name
+ 3;
689 new_ldlinux_name
[0] = dev_fd
| 0x40;
690 new_ldlinux_name
[1] = ':';
691 new_ldlinux_name
[2] = '\\';
693 for (sd
= opt
.directory
; *sd
; sd
++) {
696 if (c
== '/' || c
== '\\') {
708 /* Skip if subdirectory == root */
709 if (cp
> new_ldlinux_name
+ 3) {
713 memcpy(cp
, "ldlinux.sys", 12);
715 set_attributes(ldlinux_name
, 0);
716 if (rename(ldlinux_name
, new_ldlinux_name
))
717 set_attributes(ldlinux_name
, 0x07);
719 set_attributes(new_ldlinux_name
, 0x07);
724 * Patch ldlinux.sys and the boot sector
726 i
= syslinux_patch(sectors
, nsectors
, opt
.stupid_mode
, opt
.raid_mode
, opt
.directory
, NULL
);
727 patch_sectors
= (i
+ SECTOR_SIZE
- 1) >> SECTOR_SHIFT
;
730 * Overwrite the now-patched ldlinux.sys
732 /* lock_device(3); -- doesn't seem to be needed */
733 dp
= syslinux_ldlinux
;
734 for (i
= 0; i
< patch_sectors
; i
++) {
735 memcpy_from_sl(sectbuf
, dp
, SECTOR_SIZE
);
737 write_device(dev_fd
, sectbuf
, 1, sectors
[i
]);
741 * Muck with the MBR, if desired, while we hold the lock
743 adjust_mbr(dev_fd
, opt
.install_mbr
, opt
.activate_partition
);
746 * To finish up, write the boot sector
749 /* Read the superblock again since it might have changed while mounted */
750 read_device(dev_fd
, sectbuf
, 1, 0);
752 /* Copy the syslinux code into the boot sector */
753 syslinux_make_bootsect(sectbuf
, VFAT
);
755 /* Write new boot sector */
756 if (opt
.bootsecfile
) {
758 fd
= creat(opt
.bootsecfile
, 0x20); /* ARCHIVE */
759 write_file(fd
, sectbuf
, SECTOR_SIZE
);
762 write_device(dev_fd
, sectbuf
, 1, 0);