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
17 * This is Linux-specific by now.
19 * This is an alternate version of the installer which doesn't require
20 * mtools, but requires root privilege.
24 * If DO_DIRECT_MOUNT is 0, call mount(8)
25 * If DO_DIRECT_MOUNT is 1, call mount(2)
28 # define DO_DIRECT_MOUNT 1
30 # define DO_DIRECT_MOUNT 0 /* glibc has broken losetup ioctls */
34 #define _XOPEN_SOURCE 500 /* For pread() pwrite() */
35 #define _FILE_OFFSET_BITS 64
46 #include <sys/types.h>
48 #include <sys/mount.h>
50 #include "linuxioctl.h"
54 # define _PATH_MOUNT "/bin/mount"
57 # define _PATH_UMOUNT "/bin/umount"
60 # define _PATH_TMP "/tmp/"
66 # include <linux/loop.h>
73 #include "syslxopt.h" /* unified options */
75 extern const char *program
; /* Name of program */
78 char *mntpath
= NULL
; /* Path on which to mount */
81 int loop_fd
= -1; /* Loop device */
84 void __attribute__ ((noreturn
)) die(const char *msg
)
86 fprintf(stderr
, "%s: %s\n", program
, msg
);
90 ioctl(loop_fd
, LOOP_CLR_FD
, 0); /* Free loop device */
105 int do_mount(int dev_fd
, int *cookie
, const char *mntpath
, const char *fstype
)
111 if (fstat(dev_fd
, &st
) < 0)
116 if (!S_ISBLK(st
.st_mode
)) {
117 /* It's file, need to mount it loopback */
119 struct loop_info64 loopinfo
;
122 for (n
= 0; loop_fd
< 0; n
++) {
123 snprintf(devfdname
, sizeof devfdname
, "/dev/loop%u", n
);
124 loop_fd
= open(devfdname
, O_RDWR
);
125 if (loop_fd
< 0 && errno
== ENOENT
) {
126 die("no available loopback device!");
128 if (ioctl(loop_fd
, LOOP_SET_FD
, (void *)dev_fd
)) {
132 die("cannot set up loopback device");
137 if (ioctl(loop_fd
, LOOP_GET_STATUS64
, &loopinfo
) ||
138 (loopinfo
.lo_offset
= opt
.offset
,
139 ioctl(loop_fd
, LOOP_SET_STATUS64
, &loopinfo
)))
140 die("cannot set up loopback device");
145 snprintf(devfdname
, sizeof devfdname
, "/proc/%lu/fd/%d",
146 (unsigned long)mypid
, dev_fd
);
150 return mount(devfdname
, mntpath
, fstype
,
151 MS_NOEXEC
| MS_NOSUID
, "umask=077,quiet");
155 char devfdname
[128], mnt_opts
[128];
159 snprintf(devfdname
, sizeof devfdname
, "/proc/%lu/fd/%d",
160 (unsigned long)mypid
, dev_fd
);
166 if (!S_ISBLK(st
.st_mode
)) {
167 snprintf(mnt_opts
, sizeof mnt_opts
,
168 "rw,nodev,noexec,loop,offset=%llu,umask=077,quiet",
169 (unsigned long long)opt
.offset
);
171 snprintf(mnt_opts
, sizeof mnt_opts
,
172 "rw,nodev,noexec,umask=077,quiet");
174 execl(_PATH_MOUNT
, _PATH_MOUNT
, "-t", fstype
, "-o", mnt_opts
,
175 devfdname
, mntpath
, NULL
);
176 _exit(255); /* execl failed */
179 w
= waitpid(f
, &status
, 0);
180 return (w
!= f
|| status
) ? -1 : 0;
188 void do_umount(const char *mntpath
, int cookie
)
191 int loop_fd
= cookie
;
193 if (umount2(mntpath
, 0))
194 die("could not umount path");
197 ioctl(loop_fd
, LOOP_CLR_FD
, 0); /* Free loop device */
211 execl(_PATH_UMOUNT
, _PATH_UMOUNT
, mntpath
, NULL
);
214 w
= waitpid(f
, &status
, 0);
215 if (w
!= f
|| status
) {
222 * Modify the ADV of an existing installation
224 int modify_existing_adv(const char *path
)
227 syslinux_reset_adv(syslinux_adv
);
228 else if (read_adv(path
, "ldlinux.sys") < 0)
231 if (modify_adv() < 0)
234 if (write_adv(path
, "ldlinux.sys") < 0)
240 int main(int argc
, char *argv
[])
242 static unsigned char sectbuf
[SECTOR_SIZE
];
250 sector_t
*sectors
= NULL
;
251 int ldlinux_sectors
= (boot_image_len
+ SECTOR_SIZE
- 1) >> SECTOR_SHIFT
;
259 parse_options(argc
, argv
, MODE_SYSLINUX
);
261 /* Note: subdir is guaranteed to start and end in / */
262 if (opt
.directory
&& opt
.directory
[0]) {
263 int len
= strlen(opt
.directory
);
264 int rv
= asprintf(&subdir
, "%s%s%s",
265 opt
.directory
[0] == '/' ? "" : "/",
267 opt
.directory
[len
-1] == '/' ? "" : "/");
268 if (rv
< 0 || !subdir
) {
276 if (!opt
.device
|| opt
.install_mbr
|| opt
.activate_partition
|| opt
.force
)
277 usage(EX_USAGE
, MODE_SYSLINUX
);
280 * First make sure we can open the device at all, and that we have
281 * read/write permission.
283 dev_fd
= open(opt
.device
, O_RDWR
);
284 if (dev_fd
< 0 || fstat(dev_fd
, &st
) < 0) {
289 if (!S_ISBLK(st
.st_mode
) && !S_ISREG(st
.st_mode
) && !S_ISCHR(st
.st_mode
)) {
290 die("not a device or regular file");
293 if (opt
.offset
&& S_ISBLK(st
.st_mode
)) {
294 die("can't combine an offset with a block device");
298 xpread(dev_fd
, sectbuf
, SECTOR_SIZE
, opt
.offset
);
302 * Check to see that what we got was indeed an MS-DOS boot sector/superblock
304 if ((errmsg
= syslinux_check_bootsect(sectbuf
))) {
305 fprintf(stderr
, "%s: %s\n", opt
.device
, errmsg
);
310 * Now mount the device.
313 die("This program needs root privilege");
319 /* We're root or at least setuid.
320 Make a temp dir and pass all the gunky options to mount. */
322 if (chdir(_PATH_TMP
)) {
323 fprintf(stderr
, "%s: Cannot access the %s directory.\n",
327 #define TMP_MODE (S_IXUSR|S_IWUSR|S_IXGRP|S_IWGRP|S_IWOTH|S_IXOTH|S_ISVTX)
329 if (stat(".", &dst
) || !S_ISDIR(dst
.st_mode
) ||
330 (dst
.st_mode
& TMP_MODE
) != TMP_MODE
) {
331 die("possibly unsafe " _PATH_TMP
" permissions");
335 snprintf(mntname
, sizeof mntname
, "syslinux.mnt.%lu.%d",
336 (unsigned long)mypid
, i
);
338 if (lstat(mntname
, &dst
) != -1 || errno
!= ENOENT
)
341 rv
= mkdir(mntname
, 0000);
344 if (errno
== EEXIST
|| errno
== EINTR
)
350 if (lstat(mntname
, &dst
) || dst
.st_mode
!= (S_IFDIR
| 0000) ||
352 die("someone is trying to symlink race us!");
354 break; /* OK, got something... */
360 if (do_mount(dev_fd
, &mnt_cookie
, mntpath
, "vfat") &&
361 do_mount(dev_fd
, &mnt_cookie
, mntpath
, "msdos")) {
366 ldlinux_path
= alloca(strlen(mntpath
) + strlen(subdir
) + 1);
367 sprintf(ldlinux_path
, "%s%s", mntpath
, subdir
);
369 ldlinux_name
= alloca(strlen(ldlinux_path
) + 14);
375 sprintf(ldlinux_name
, "%sldlinux.sys", ldlinux_path
);
377 /* update ADV only ? */
378 if (opt
.update_only
== -1) {
379 if (opt
.reset_adv
|| opt
.set_once
) {
380 modify_existing_adv(ldlinux_path
);
381 do_umount(mntpath
, mnt_cookie
);
386 fprintf(stderr
, "%s: please specify --install or --update for the future\n", argv
[0]);
391 /* Read a pre-existing ADV, if already installed */
393 syslinux_reset_adv(syslinux_adv
);
394 else if (read_adv(ldlinux_path
, "ldlinux.sys") < 0)
395 syslinux_reset_adv(syslinux_adv
);
396 if (modify_adv() < 0)
399 if ((fd
= open(ldlinux_name
, O_RDONLY
)) >= 0) {
400 uint32_t zero_attr
= 0;
401 ioctl(fd
, FAT_IOCTL_SET_ATTRIBUTES
, &zero_attr
);
405 unlink(ldlinux_name
);
406 fd
= open(ldlinux_name
, O_WRONLY
| O_CREAT
| O_TRUNC
, 0444);
413 /* Write it the first time */
414 if (xpwrite(fd
, boot_image
, boot_image_len
, 0) != (int)boot_image_len
||
415 xpwrite(fd
, syslinux_adv
, 2 * ADV_SIZE
,
416 boot_image_len
) != 2 * ADV_SIZE
) {
417 fprintf(stderr
, "%s: write failure on %s\n", program
, ldlinux_name
);
426 uint32_t attr
= 0x07; /* Hidden+System+Readonly */
427 ioctl(fd
, FAT_IOCTL_SET_ATTRIBUTES
, &attr
);
431 * Create a block map.
433 ldlinux_sectors
+= 2; /* 2 ADV sectors */
434 sectors
= calloc(ldlinux_sectors
, sizeof *sectors
);
435 if (sectmap(fd
, sectors
, ldlinux_sectors
)) {
443 do_umount(mntpath
, mnt_cookie
);
451 * Patch ldlinux.sys and the boot sector
453 i
= syslinux_patch(sectors
, ldlinux_sectors
, opt
.stupid_mode
,
454 opt
.raid_mode
, subdir
, NULL
);
455 patch_sectors
= (i
+ SECTOR_SIZE
- 1) >> SECTOR_SHIFT
;
458 * Write the now-patched first sectors of ldlinux.sys
460 for (i
= 0; i
< patch_sectors
; i
++) {
461 xpwrite(dev_fd
, boot_image
+ i
* SECTOR_SIZE
, SECTOR_SIZE
,
462 opt
.offset
+ ((off_t
) sectors
[i
] << SECTOR_SHIFT
));
466 * To finish up, write the boot sector
469 /* Read the superblock again since it might have changed while mounted */
470 xpread(dev_fd
, sectbuf
, SECTOR_SIZE
, opt
.offset
);
472 /* Copy the syslinux code into the boot sector */
473 syslinux_make_bootsect(sectbuf
);
475 /* Write new boot sector */
476 xpwrite(dev_fd
, sectbuf
, SECTOR_SIZE
, opt
.offset
);