2 /* ----------------------------------------------------------------------- *
4 * Copyright 1998-2004 H. Peter Anvin - All Rights Reserved
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
17 * This program ought to be portable. I hope so, at least.
19 * This is an alternate version of the installer which doesn't require
20 * mtools, but requires root privilege.
24 # define DO_DIRECT_MOUNT 1 /* Call mount(2) directly */
26 # define DO_DIRECT_MOUNT 0 /* Call /bin/mount instead */
30 #define _XOPEN_SOURCE 500 /* For pread() pwrite() */
31 #define _FILE_OFFSET_BITS 64
42 #include <sys/types.h>
44 #include <sys/mount.h>
51 # include <linux/loop.h>
57 # define _PATH_MOUNT "/bin/mount"
60 # define _PATH_UMOUNT "/bin/umount"
65 const char *program; /* Name of program */
66 const char *device; /* Device to install to */
68 char *mntpath = NULL; /* Path on which to mount */
70 int loop_fd = -1; /* Loop device */
73 void __attribute__((noreturn)) usage(void)
75 fprintf(stderr, "Usage: %s [-sf] [-o offset] device\n", program);
79 void __attribute__((noreturn)) die(const char *msg)
81 fprintf(stderr, "%s: %s\n", program, msg);
84 if ( loop_fd != -1 ) {
85 ioctl(loop_fd, LOOP_CLR_FD, 0); /* Free loop device */
98 * read/write wrapper functions
100 ssize_t xpread(int fd, void *buf, size_t count, off_t offset)
106 rv = pread(fd, buf, count, offset);
109 } else if ( rv == -1 ) {
110 if ( errno == EINTR ) {
126 ssize_t xpwrite(int fd, void *buf, size_t count, off_t offset)
132 rv = pwrite(fd, buf, count, offset);
135 } else if ( rv == -1 ) {
136 if ( errno == EINTR ) {
153 * Version of the read function suitable for libfat
155 int libfat_xpread(intptr_t pp, void *buf, size_t secsize, libfat_sector_t sector)
157 off_t offset = (off_t)sector * secsize;
158 return xpread(pp, buf, secsize, offset);
161 int main(int argc, char *argv[])
163 static unsigned char sectbuf[512];
165 const unsigned char *cdp;
172 char mntname[64], devfdname[64];
173 char *ldlinux_name, **argp, *opt;
174 int force = 0; /* -f (force) option */
175 off_t offset = 0; /* -o (offset) option */
176 struct libfat_filesystem *fs;
177 libfat_sector_t s, *secp, sectors[65]; /* 65 is maximum possible */
178 int32_t ldlinux_cluster;
181 (void)argc; /* Unused */
190 for ( argp = argv+1 ; *argp ; argp++ ) {
191 if ( **argp == '-' ) {
198 syslinux_make_stupid(); /* Use "safe, slow and stupid" code */
199 } else if ( *opt == 'f' ) {
200 force = 1; /* Force install */
201 } else if ( *opt == 'o' && argp[1] ) {
202 offset = (off_t)strtoull(*++argp, NULL, 0); /* Byte offset */
219 * First make sure we can open the device at all, and that we have
220 * read/write permission.
222 dev_fd = open(device, O_RDWR);
223 if ( dev_fd < 0 || fstat(dev_fd, &st) < 0 ) {
228 if ( !force && !S_ISBLK(st.st_mode) && !S_ISREG(st.st_mode) ) {
229 die("not a block device or regular file (use -f to override)");
232 if ( !force && offset != 0 && !S_ISREG(st.st_mode) ) {
233 die("not a regular file and an offset specified (use -f to override)");
236 xpread(dev_fd, sectbuf, 512, offset);
240 * Check to see that what we got was indeed an MS-DOS boot sector/superblock
242 if(!syslinux_check_bootsect(sectbuf,device)) {
247 * Now mount the device.
250 die("This program needs root privilege");
256 /* We're root or at least setuid.
257 Make a temp dir and pass all the gunky options to mount. */
259 if ( chdir("/tmp") ) {
264 #define TMP_MODE (S_IXUSR|S_IWUSR|S_IXGRP|S_IWGRP|S_IWOTH|S_IXOTH|S_ISVTX)
266 if ( stat(".", &dst) || !S_ISDIR(dst.st_mode) ||
267 (dst.st_mode & TMP_MODE) != TMP_MODE ) {
268 die("possibly unsafe /tmp permissions");
271 for ( i = 0 ; ; i++ ) {
272 snprintf(mntname, sizeof mntname, "syslinux.mnt.%lu.%d",
273 (unsigned long)mypid, i);
275 if ( lstat(mntname, &dst) != -1 || errno != ENOENT )
278 rv = mkdir(mntname, 0000);
281 if ( errno == EEXIST || errno == EINTR )
287 if ( lstat(mntname, &dst) || dst.st_mode != (S_IFDIR|0000) ||
289 die("someone is trying to symlink race us!");
291 break; /* OK, got something... */
297 if ( S_ISREG(st.st_mode) ) {
298 /* It's file, need to mount it loopback */
300 struct loop_info64 loopinfo;
302 for ( n = 0 ; loop_fd < 0 ; n++ ) {
303 snprintf(devfdname, sizeof devfdname, "/dev/loop%u", n);
304 loop_fd = open(devfdname, O_RDWR);
305 if ( loop_fd < 0 && errno == ENOENT ) {
306 die("no available loopback device!");
308 if ( ioctl(loop_fd, LOOP_SET_FD, (void *)dev_fd) ) {
309 close(loop_fd); loop_fd = -1;
310 if ( errno != EBUSY )
311 die("cannot set up loopback device");
316 if ( ioctl(loop_fd, LOOP_GET_STATUS64, &loopinfo) ||
317 (loopinfo.lo_offset = offset,
318 ioctl(loop_fd, LOOP_SET_STATUS64, &loopinfo)) )
319 die("cannot set up loopback device");
322 snprintf(devfdname, sizeof devfdname, "/proc/%lu/fd/%d",
323 (unsigned long)mypid, dev_fd);
326 if ( mount(devfdname, mntpath, "msdos",
327 MS_NOEXEC|MS_NOSUID, "umask=077,quiet") )
328 die("could not mount filesystem");
332 snprintf(devfdname, sizeof devfdname, "/proc/%lu/fd/%d",
333 (unsigned long)mypid, dev_fd);
340 } else if ( f == 0 ) {
342 if ( S_ISREG(st.st_mode) ) {
343 snprintf(mnt_opts, sizeof mnt_opts, "rw,nodev,noexec,loop,offset=%llu,umask=077,quiet",
344 (unsigned long long)offset);
346 snprintf(mnt_opts, sizeof mnt_opts, "rw,nodev,noexec,umask=077,quiet");
348 execl(_PATH_MOUNT, _PATH_MOUNT, "-t", "msdos", "-o", mnt_opts,\
349 devfdname, mntpath, NULL);
350 _exit(255); /* execl failed */
353 w = waitpid(f, &status, 0);
354 if ( w != f || status ) {
356 exit(1); /* Mount failed */
362 ldlinux_name = alloca(strlen(mntpath)+13);
363 if ( !ldlinux_name ) {
368 sprintf(ldlinux_name, "%s/ldlinux.sys", mntpath);
370 unlink(ldlinux_name);
371 fd = open(ldlinux_name, O_WRONLY|O_CREAT|O_TRUNC, 0444);
378 cdp = syslinux_ldlinux;
379 left = syslinux_ldlinux_len;
381 nb = write(fd, cdp, left);
382 if ( nb == -1 && errno == EINTR )
384 else if ( nb <= 0 ) {
395 * I don't understand why I need this. Does the DOS filesystems
396 * not honour the mode passed to open()?
407 if ( umount2(mntpath, 0) )
408 die("could not umount path");
410 if ( loop_fd != -1 ) {
411 ioctl(loop_fd, LOOP_CLR_FD, 0); /* Free loop device */
422 } else if ( f == 0 ) {
423 execl(_PATH_UMOUNT, _PATH_UMOUNT, mntpath, NULL);
426 w = waitpid(f, &status, 0);
427 if ( w != f || status ) {
440 * Now, use libfat to create a block map. This probably
441 * should be changed to use ioctl(...,FIBMAP,...) since
442 * this is supposed to be a simple, privileged version
445 fs = libfat_open(libfat_xpread, dev_fd);
446 ldlinux_cluster = libfat_searchdir(fs, 0, "LDLINUX SYS", NULL);
449 s = libfat_clustertosector(fs, ldlinux_cluster);
450 while ( s && nsectors < 65 ) {
453 s = libfat_nextsector(fs, s);
458 * Patch ldlinux.sys and the boot sector
460 syslinux_patch(sectors, nsectors);
463 * Write the now-patched first sector of ldlinux.sys
465 xpwrite(dev_fd, syslinux_ldlinux, 512, offset + ((off_t)sectors[0] << 9));
468 * To finish up, write the boot sector
471 /* Read the superblock again since it might have changed while mounted */
472 xpread(dev_fd, sectbuf, 512, offset);
474 /* Copy the syslinux code into the boot sector */
475 syslinux_make_bootsect(sectbuf);
477 /* Write new boot sector */
478 xpwrite(dev_fd, sectbuf, 512, offset);