zzjson: Adding zzjson 1.1.0
[hdt-cyring.git] / linux / syslinux.c
blob97b6a306a4175313c5eab28793744240e8ea9562
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)
27 #ifdef __KLIBC__
28 # define DO_DIRECT_MOUNT 1
29 #else
30 # define DO_DIRECT_MOUNT 0 /* glibc has broken losetup ioctls */
31 #endif
33 #define _GNU_SOURCE
34 #define _XOPEN_SOURCE 500 /* For pread() pwrite() */
35 #define _FILE_OFFSET_BITS 64
36 #include <alloca.h>
37 #include <errno.h>
38 #include <fcntl.h>
39 #include <paths.h>
40 #include <stdio.h>
41 #include <string.h>
42 #include <stdlib.h>
43 #include <unistd.h>
44 #include <inttypes.h>
45 #include <sys/stat.h>
46 #include <sys/types.h>
47 #include <sys/wait.h>
48 #include <sys/mount.h>
50 #include "linuxioctl.h"
52 #include <paths.h>
53 #ifndef _PATH_MOUNT
54 # define _PATH_MOUNT "/bin/mount"
55 #endif
56 #ifndef _PATH_UMOUNT
57 # define _PATH_UMOUNT "/bin/umount"
58 #endif
59 #ifndef _PATH_TMP
60 # define _PATH_TMP "/tmp/"
61 #endif
63 #include "syslinux.h"
65 #if DO_DIRECT_MOUNT
66 # include <linux/loop.h>
67 #endif
69 #include <getopt.h>
70 #include <sysexits.h>
71 #include "syslxcom.h"
72 #include "setadv.h"
73 #include "syslxopt.h" /* unified options */
75 extern const char *program; /* Name of program */
77 pid_t mypid;
78 char *mntpath = NULL; /* Path on which to mount */
80 #if DO_DIRECT_MOUNT
81 int loop_fd = -1; /* Loop device */
82 #endif
84 void __attribute__ ((noreturn)) die(const char *msg)
86 fprintf(stderr, "%s: %s\n", program, msg);
88 #if DO_DIRECT_MOUNT
89 if (loop_fd != -1) {
90 ioctl(loop_fd, LOOP_CLR_FD, 0); /* Free loop device */
91 close(loop_fd);
92 loop_fd = -1;
94 #endif
96 if (mntpath)
97 unlink(mntpath);
99 exit(1);
103 * Mount routine
105 int do_mount(int dev_fd, int *cookie, const char *mntpath, const char *fstype)
107 struct stat st;
109 (void)cookie;
111 if (fstat(dev_fd, &st) < 0)
112 return errno;
114 #if DO_DIRECT_MOUNT
116 if (!S_ISBLK(st.st_mode)) {
117 /* It's file, need to mount it loopback */
118 unsigned int n = 0;
119 struct loop_info64 loopinfo;
120 int loop_fd;
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)) {
129 close(loop_fd);
130 loop_fd = -1;
131 if (errno != EBUSY)
132 die("cannot set up loopback device");
133 else
134 continue;
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");
143 *cookie = loop_fd;
144 } else {
145 snprintf(devfdname, sizeof devfdname, "/proc/%lu/fd/%d",
146 (unsigned long)mypid, dev_fd);
147 *cookie = -1;
150 return mount(devfdname, mntpath, fstype,
151 MS_NOEXEC | MS_NOSUID, "umask=077,quiet");
153 #else
155 char devfdname[128], mnt_opts[128];
156 pid_t f, w;
157 int status;
159 snprintf(devfdname, sizeof devfdname, "/proc/%lu/fd/%d",
160 (unsigned long)mypid, dev_fd);
162 f = fork();
163 if (f < 0) {
164 return -1;
165 } else if (f == 0) {
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);
170 } else {
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;
182 #endif
186 * umount routine
188 void do_umount(const char *mntpath, int cookie)
190 #if DO_DIRECT_MOUNT
191 int loop_fd = cookie;
193 if (umount2(mntpath, 0))
194 die("could not umount path");
196 if (loop_fd != -1) {
197 ioctl(loop_fd, LOOP_CLR_FD, 0); /* Free loop device */
198 close(loop_fd);
199 loop_fd = -1;
201 #else
202 pid_t f = fork();
203 pid_t w;
204 int status;
205 (void)cookie;
207 if (f < 0) {
208 perror("fork");
209 exit(1);
210 } else if (f == 0) {
211 execl(_PATH_UMOUNT, _PATH_UMOUNT, mntpath, NULL);
214 w = waitpid(f, &status, 0);
215 if (w != f || status) {
216 exit(1);
218 #endif
222 * Modify the ADV of an existing installation
224 int modify_existing_adv(const char *path)
226 if (opt.reset_adv)
227 syslinux_reset_adv(syslinux_adv);
228 else if (read_adv(path, "ldlinux.sys") < 0)
229 return 1;
231 if (modify_adv() < 0)
232 return 1;
234 if (write_adv(path, "ldlinux.sys") < 0)
235 return 1;
237 return 0;
240 int main(int argc, char *argv[])
242 static unsigned char sectbuf[SECTOR_SIZE];
243 int dev_fd, fd;
244 struct stat st;
245 int err = 0;
246 char mntname[128];
247 char *ldlinux_name;
248 char *ldlinux_path;
249 char *subdir;
250 sector_t *sectors = NULL;
251 int ldlinux_sectors = (boot_image_len + SECTOR_SIZE - 1) >> SECTOR_SHIFT;
252 const char *errmsg;
253 int mnt_cookie;
254 int patch_sectors;
255 int i;
257 mypid = getpid();
258 umask(077);
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] == '/' ? "" : "/",
266 opt.directory,
267 opt.directory[len-1] == '/' ? "" : "/");
268 if (rv < 0 || !subdir) {
269 perror(program);
270 exit(1);
272 } else {
273 subdir = "/";
276 if (!opt.device || opt.install_mbr || opt.activate_partition)
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) {
285 perror(opt.device);
286 exit(1);
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");
297 fs_type = VFAT;
298 xpread(dev_fd, sectbuf, SECTOR_SIZE, opt.offset);
299 fsync(dev_fd);
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);
306 exit(1);
310 * Now mount the device.
312 if (geteuid()) {
313 die("This program needs root privilege");
314 } else {
315 int i = 0;
316 struct stat dst;
317 int rv;
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",
324 program, _PATH_TMP);
325 exit(1);
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");
334 for (i = 0;; i++) {
335 snprintf(mntname, sizeof mntname, "syslinux.mnt.%lu.%d",
336 (unsigned long)mypid, i);
338 if (lstat(mntname, &dst) != -1 || errno != ENOENT)
339 continue;
341 rv = mkdir(mntname, 0000);
343 if (rv == -1) {
344 if (errno == EEXIST || errno == EINTR)
345 continue;
346 perror(program);
347 exit(1);
350 if (lstat(mntname, &dst) || dst.st_mode != (S_IFDIR | 0000) ||
351 dst.st_uid != 0) {
352 die("someone is trying to symlink race us!");
354 break; /* OK, got something... */
357 mntpath = mntname;
360 if (do_mount(dev_fd, &mnt_cookie, mntpath, "vfat") &&
361 do_mount(dev_fd, &mnt_cookie, mntpath, "msdos")) {
362 rmdir(mntpath);
363 die("mount failed");
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);
370 if (!ldlinux_name) {
371 perror(program);
372 err = 1;
373 goto umount;
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);
382 sync();
383 rmdir(mntpath);
384 exit(0);
385 } else {
386 fprintf(stderr, "%s: please specify --install or --update for the future\n", argv[0]);
387 opt.update_only = 0;
391 /* Read a pre-existing ADV, if already installed */
392 if (opt.reset_adv)
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)
397 exit(1);
399 if ((fd = open(ldlinux_name, O_RDONLY)) >= 0) {
400 uint32_t zero_attr = 0;
401 ioctl(fd, FAT_IOCTL_SET_ATTRIBUTES, &zero_attr);
402 close(fd);
405 unlink(ldlinux_name);
406 fd = open(ldlinux_name, O_WRONLY | O_CREAT | O_TRUNC, 0444);
407 if (fd < 0) {
408 perror(opt.device);
409 err = 1;
410 goto umount;
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);
418 exit(1);
421 fsync(fd);
423 * Set the attributes
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)) {
436 perror("bmap");
437 exit(1);
439 close(fd);
440 sync();
442 umount:
443 do_umount(mntpath, mnt_cookie);
444 sync();
445 rmdir(mntpath);
447 if (err)
448 exit(err);
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);
478 close(dev_fd);
479 sync();
481 /* Done! */
483 return 0;