1 /* ----------------------------------------------------------------------- *
3 * Copyright 1998-2008 H. Peter Anvin - All Rights Reserved
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation, Inc., 53 Temple Place Ste 330,
8 * Boston MA 02111-1307, USA; either version 2 of the License, or
9 * (at your option) any later version; incorporated herein by reference.
11 * ----------------------------------------------------------------------- */
14 * syslinux.c - Linux installer program for SYSLINUX
16 * This is Linux-specific by now.
18 * This is an alternate version of the installer which doesn't require
19 * mtools, but requires root privilege.
23 * If DO_DIRECT_MOUNT is 0, call mount(8)
24 * If DO_DIRECT_MOUNT is 1, call mount(2)
27 # define DO_DIRECT_MOUNT 1
29 # define DO_DIRECT_MOUNT 0 /* glibc has broken losetup ioctls */
33 #define _XOPEN_SOURCE 500 /* For pread() pwrite() */
34 #define _FILE_OFFSET_BITS 64
45 #include <sys/types.h>
47 #include <sys/mount.h>
49 #include <sys/ioctl.h>
50 #include <linux/fs.h> /* FIGETBSZ, FIBMAP */
51 #include <linux/msdos_fs.h> /* FAT_IOCTL_SET_ATTRIBUTES */
52 #ifndef FAT_IOCTL_SET_ATTRIBUTES
53 # define FAT_IOCTL_SET_ATTRIBUTES _IOW('r', 0x11, uint32_t)
60 # define _PATH_MOUNT "/bin/mount"
63 # define _PATH_UMOUNT "/bin/umount"
66 # define _PATH_TMP "/tmp/"
72 # include <linux/loop.h>
75 const char *program; /* Name of program */
76 const char *device; /* Device to install to */
78 char *mntpath = NULL; /* Path on which to mount */
79 off_t filesystem_offset = 0; /* Filesystem offset */
81 int loop_fd = -1; /* Loop device */
84 void __attribute__((noreturn)) usage(void)
86 fprintf(stderr, "Usage: %s [-sfr][-d directory][-o offset] device\n", program);
90 void __attribute__((noreturn)) die(const char *msg)
92 fprintf(stderr, "%s: %s\n", program, msg);
95 if ( loop_fd != -1 ) {
96 ioctl(loop_fd, LOOP_CLR_FD, 0); /* Free loop device */
109 * read/write wrapper functions
111 ssize_t xpread(int fd, void *buf, size_t count, off_t offset)
113 char *bufp = (char *)buf;
118 rv = pread(fd, bufp, count, offset);
121 } else if ( rv == -1 ) {
122 if ( errno == EINTR ) {
125 die(strerror(errno));
138 ssize_t xpwrite(int fd, const void *buf, size_t count, off_t offset)
140 const char *bufp = (const char *)buf;
145 rv = pwrite(fd, bufp, count, offset);
148 } else if ( rv == -1 ) {
149 if ( errno == EINTR ) {
152 die(strerror(errno));
166 * Create a block map for ldlinux.sys
168 int make_block_map(uint32_t *sectors, int len, int dev_fd, int fd)
171 int blocksize, nblock, block;
176 if (ioctl(fd, FIGETBSZ, &blocksize) < 0)
177 die("ioctl FIGETBSZ failed");
179 blocksize >>= SECTOR_SHIFT; /* sectors/block */
184 if (ioctl(fd, FIBMAP, &block) < 0)
185 die("ioctl FIBMAP failed");
187 for (i = 0; i < blocksize; i++) {
191 *sectors++ = (block*blocksize)+i;
193 len -= (1 << SECTOR_SHIFT);
203 int do_mount(int dev_fd, int *cookie, const char *mntpath, const char *fstype)
209 if (fstat(dev_fd, &st) < 0)
214 if ( !S_ISBLK(st.st_mode) ) {
215 /* It's file, need to mount it loopback */
217 struct loop_info64 loopinfo;
220 for ( n = 0 ; loop_fd < 0 ; n++ ) {
221 snprintf(devfdname, sizeof devfdname, "/dev/loop%u", n);
222 loop_fd = open(devfdname, O_RDWR);
223 if ( loop_fd < 0 && errno == ENOENT ) {
224 die("no available loopback device!");
226 if ( ioctl(loop_fd, LOOP_SET_FD, (void *)dev_fd) ) {
227 close(loop_fd); loop_fd = -1;
228 if ( errno != EBUSY )
229 die("cannot set up loopback device");
234 if ( ioctl(loop_fd, LOOP_GET_STATUS64, &loopinfo) ||
235 (loopinfo.lo_offset = filesystem_offset,
236 ioctl(loop_fd, LOOP_SET_STATUS64, &loopinfo)) )
237 die("cannot set up loopback device");
242 snprintf(devfdname, sizeof devfdname, "/proc/%lu/fd/%d",
243 (unsigned long)mypid, dev_fd);
247 return mount(devfdname, mntpath, fstype,
248 MS_NOEXEC|MS_NOSUID, "umask=077,quiet");
252 char devfdname[128], mnt_opts[128];
256 snprintf(devfdname, sizeof devfdname, "/proc/%lu/fd/%d",
257 (unsigned long)mypid, dev_fd);
262 } else if ( f == 0 ) {
263 if ( !S_ISBLK(st.st_mode) ) {
264 snprintf(mnt_opts, sizeof mnt_opts,
265 "rw,nodev,noexec,loop,offset=%llu,umask=077,quiet",
266 (unsigned long long)filesystem_offset);
268 snprintf(mnt_opts, sizeof mnt_opts, "rw,nodev,noexec,umask=077,quiet");
270 execl(_PATH_MOUNT, _PATH_MOUNT, "-t", fstype, "-o", mnt_opts, \
271 devfdname, mntpath, NULL);
272 _exit(255); /* execl failed */
275 w = waitpid(f, &status, 0);
276 return ( w != f || status ) ? -1 : 0;
284 void do_umount(const char *mntpath, int cookie)
287 int loop_fd = cookie;
289 if ( umount2(mntpath, 0) )
290 die("could not umount path");
292 if ( loop_fd != -1 ) {
293 ioctl(loop_fd, LOOP_CLR_FD, 0); /* Free loop device */
307 } else if ( f == 0 ) {
308 execl(_PATH_UMOUNT, _PATH_UMOUNT, mntpath, NULL);
311 w = waitpid(f, &status, 0);
312 if ( w != f || status ) {
318 int main(int argc, char *argv[])
320 static unsigned char sectbuf[SECTOR_SIZE];
322 const unsigned char *cdp;
328 char *ldlinux_name, **argp, *opt;
329 const char *subdir = NULL;
338 int force = 0; /* -f (force) option */
339 int stupid = 0; /* -s (stupid) option */
340 int raid_mode = 0; /* -r (RAID) option */
342 (void)argc; /* Unused */
351 for ( argp = argv+1 ; *argp ; argp++ ) {
352 if ( **argp == '-' ) {
360 } else if ( *opt == 'r' ) {
362 } else if ( *opt == 'f' ) {
363 force = 1; /* Force install */
364 } else if ( *opt == 'd' && argp[1] ) {
366 } else if ( *opt == 'o' && argp[1] ) {
368 filesystem_offset = (off_t)strtoull(*++argp, NULL, 0);
385 * First make sure we can open the device at all, and that we have
386 * read/write permission.
388 dev_fd = open(device, O_RDWR);
389 if ( dev_fd < 0 || fstat(dev_fd, &st) < 0 ) {
394 if ( !S_ISBLK(st.st_mode) && !S_ISREG(st.st_mode) && !S_ISCHR(st.st_mode) ) {
395 die("not a device or regular file");
398 if ( filesystem_offset && S_ISBLK(st.st_mode) ) {
399 die("can't combine an offset with a block device");
402 xpread(dev_fd, sectbuf, SECTOR_SIZE, filesystem_offset);
406 * Check to see that what we got was indeed an MS-DOS boot sector/superblock
408 if( (errmsg = syslinux_check_bootsect(sectbuf)) ) {
409 fprintf(stderr, "%s: %s\n", device, errmsg);
414 * Now mount the device.
417 die("This program needs root privilege");
423 /* We're root or at least setuid.
424 Make a temp dir and pass all the gunky options to mount. */
426 if ( chdir(_PATH_TMP) ) {
431 #define TMP_MODE (S_IXUSR|S_IWUSR|S_IXGRP|S_IWGRP|S_IWOTH|S_IXOTH|S_ISVTX)
433 if ( stat(".", &dst) || !S_ISDIR(dst.st_mode) ||
434 (dst.st_mode & TMP_MODE) != TMP_MODE ) {
435 die("possibly unsafe " _PATH_TMP " permissions");
438 for ( i = 0 ; ; i++ ) {
439 snprintf(mntname, sizeof mntname, "syslinux.mnt.%lu.%d",
440 (unsigned long)mypid, i);
442 if ( lstat(mntname, &dst) != -1 || errno != ENOENT )
445 rv = mkdir(mntname, 0000);
448 if ( errno == EEXIST || errno == EINTR )
454 if ( lstat(mntname, &dst) || dst.st_mode != (S_IFDIR|0000) ||
456 die("someone is trying to symlink race us!");
458 break; /* OK, got something... */
464 if (do_mount(dev_fd, &mnt_cookie, mntpath, "vfat") &&
465 do_mount(dev_fd, &mnt_cookie, mntpath, "msdos")) {
470 ldlinux_name = alloca(strlen(mntpath)+14+
471 (subdir ? strlen(subdir)+2 : 0));
472 if ( !ldlinux_name ) {
477 sprintf(ldlinux_name, "%s%s%s//ldlinux.sys",
478 mntpath, subdir ? "//" : "", subdir ? subdir : "");
480 if ((fd = open(ldlinux_name, O_RDONLY)) >= 0) {
481 uint32_t zero_attr = 0;
482 ioctl(fd, FAT_IOCTL_SET_ATTRIBUTES, &zero_attr);
486 unlink(ldlinux_name);
487 fd = open(ldlinux_name, O_WRONLY|O_CREAT|O_TRUNC, 0444);
494 cdp = syslinux_ldlinux;
495 left = syslinux_ldlinux_len;
497 nb = write(fd, cdp, left);
498 if ( nb == -1 && errno == EINTR )
500 else if ( nb <= 0 ) {
515 uint32_t attr = 0x07; /* Hidden+System+Readonly */
516 ioctl(fd, FAT_IOCTL_SET_ATTRIBUTES, &attr);
520 * Create a block map.
522 ldlinux_sectors = (syslinux_ldlinux_len+SECTOR_SIZE-1) >> SECTOR_SHIFT;
523 sectors = calloc(ldlinux_sectors, sizeof *sectors);
524 nsectors = make_block_map(sectors, syslinux_ldlinux_len, dev_fd, fd);
530 do_umount(mntpath, mnt_cookie);
538 * Patch ldlinux.sys and the boot sector
540 i = syslinux_patch(sectors, nsectors, stupid, raid_mode);
541 patch_sectors = (i + SECTOR_SIZE - 1) >> SECTOR_SHIFT;
544 * Write the now-patched first sectors of ldlinux.sys
546 for (i = 0; i < patch_sectors; i++) {
547 xpwrite(dev_fd, syslinux_ldlinux + i*SECTOR_SIZE, SECTOR_SIZE,
548 filesystem_offset+((off_t)sectors[i] << SECTOR_SHIFT));
552 * To finish up, write the boot sector
555 /* Read the superblock again since it might have changed while mounted */
556 xpread(dev_fd, sectbuf, SECTOR_SIZE, filesystem_offset);
558 /* Copy the syslinux code into the boot sector */
559 syslinux_make_bootsect(sectbuf);
561 /* Write new boot sector */
562 xpwrite(dev_fd, sectbuf, SECTOR_SIZE, filesystem_offset);