syslinux: provide for backwards compatibility
[profile/ivi/syslinux.git] / linux / syslinux.c
1 /* ----------------------------------------------------------------------- *
2  *
3  *   Copyright 1998-2008 H. Peter Anvin - All Rights Reserved
4  *
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.
10  *
11  * ----------------------------------------------------------------------- */
12
13 /*
14  * syslinux.c - Linux installer program for SYSLINUX
15  *
16  * This is Linux-specific by now.
17  *
18  * This is an alternate version of the installer which doesn't require
19  * mtools, but requires root privilege.
20  */
21
22 /*
23  * If DO_DIRECT_MOUNT is 0, call mount(8)
24  * If DO_DIRECT_MOUNT is 1, call mount(2)
25  */
26 #ifdef __KLIBC__
27 # define DO_DIRECT_MOUNT 1
28 #else
29 # define DO_DIRECT_MOUNT 0      /* glibc has broken losetup ioctls */
30 #endif
31
32 #define _GNU_SOURCE
33 #define _XOPEN_SOURCE 500       /* For pread() pwrite() */
34 #define _FILE_OFFSET_BITS 64
35 #include <alloca.h>
36 #include <errno.h>
37 #include <fcntl.h>
38 #include <paths.h>
39 #include <stdio.h>
40 #include <string.h>
41 #include <stdlib.h>
42 #include <unistd.h>
43 #include <inttypes.h>
44 #include <sys/stat.h>
45 #include <sys/types.h>
46 #include <sys/wait.h>
47 #include <sys/mount.h>
48
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)
54 #endif
55 #undef SECTOR_SIZE
56 #undef SECTOR_SHIFT
57
58 #include <paths.h>
59 #ifndef _PATH_MOUNT
60 # define _PATH_MOUNT "/bin/mount"
61 #endif
62 #ifndef _PATH_UMOUNT
63 # define _PATH_UMOUNT "/bin/umount"
64 #endif
65 #ifndef _PATH_TMP
66 # define _PATH_TMP "/tmp/"
67 #endif
68
69 #include "syslinux.h"
70
71 #if DO_DIRECT_MOUNT
72 # include <linux/loop.h>
73 #endif
74
75 #include <getopt.h>
76 #include <sysexits.h>
77 #include "syslxcom.h"
78 #include "setadv.h"
79 #include "syslxopt.h" /* unified options */
80
81 extern const char *program;     /* Name of program */
82
83 pid_t mypid;
84 char *mntpath = NULL;           /* Path on which to mount */
85
86 /*
87  * Image file
88  */
89 #define boot_image      syslinux_ldlinux
90 #define boot_image_len  syslinux_ldlinux_len
91
92 #if DO_DIRECT_MOUNT
93 int loop_fd = -1;               /* Loop device */
94 #endif
95
96 void __attribute__ ((noreturn)) die(const char *msg)
97 {
98     fprintf(stderr, "%s: %s\n", program, msg);
99
100 #if DO_DIRECT_MOUNT
101     if (loop_fd != -1) {
102         ioctl(loop_fd, LOOP_CLR_FD, 0); /* Free loop device */
103         close(loop_fd);
104         loop_fd = -1;
105     }
106 #endif
107
108     if (mntpath)
109         unlink(mntpath);
110
111     exit(1);
112 }
113
114 /*
115  * Mount routine
116  */
117 int do_mount(int dev_fd, int *cookie, const char *mntpath, const char *fstype)
118 {
119     struct stat st;
120
121     (void)cookie;
122
123     if (fstat(dev_fd, &st) < 0)
124         return errno;
125
126 #if DO_DIRECT_MOUNT
127     {
128         if (!S_ISBLK(st.st_mode)) {
129             /* It's file, need to mount it loopback */
130             unsigned int n = 0;
131             struct loop_info64 loopinfo;
132             int loop_fd;
133
134             for (n = 0; loop_fd < 0; n++) {
135                 snprintf(devfdname, sizeof devfdname, "/dev/loop%u", n);
136                 loop_fd = open(devfdname, O_RDWR);
137                 if (loop_fd < 0 && errno == ENOENT) {
138                     die("no available loopback device!");
139                 }
140                 if (ioctl(loop_fd, LOOP_SET_FD, (void *)dev_fd)) {
141                     close(loop_fd);
142                     loop_fd = -1;
143                     if (errno != EBUSY)
144                         die("cannot set up loopback device");
145                     else
146                         continue;
147                 }
148
149                 if (ioctl(loop_fd, LOOP_GET_STATUS64, &loopinfo) ||
150                     (loopinfo.lo_offset = opt.offset,
151                      ioctl(loop_fd, LOOP_SET_STATUS64, &loopinfo)))
152                     die("cannot set up loopback device");
153             }
154
155             *cookie = loop_fd;
156         } else {
157             snprintf(devfdname, sizeof devfdname, "/proc/%lu/fd/%d",
158                      (unsigned long)mypid, dev_fd);
159             *cookie = -1;
160         }
161
162         return mount(devfdname, mntpath, fstype,
163                      MS_NOEXEC | MS_NOSUID, "umask=077,quiet");
164     }
165 #else
166     {
167         char devfdname[128], mnt_opts[128];
168         pid_t f, w;
169         int status;
170
171         snprintf(devfdname, sizeof devfdname, "/proc/%lu/fd/%d",
172                  (unsigned long)mypid, dev_fd);
173
174         f = fork();
175         if (f < 0) {
176             return -1;
177         } else if (f == 0) {
178             if (!S_ISBLK(st.st_mode)) {
179                 snprintf(mnt_opts, sizeof mnt_opts,
180                          "rw,nodev,noexec,loop,offset=%llu,umask=077,quiet",
181                          (unsigned long long)opt.offset);
182             } else {
183                 snprintf(mnt_opts, sizeof mnt_opts,
184                          "rw,nodev,noexec,umask=077,quiet");
185             }
186             execl(_PATH_MOUNT, _PATH_MOUNT, "-t", fstype, "-o", mnt_opts,
187                   devfdname, mntpath, NULL);
188             _exit(255);         /* execl failed */
189         }
190
191         w = waitpid(f, &status, 0);
192         return (w != f || status) ? -1 : 0;
193     }
194 #endif
195 }
196
197 /*
198  * umount routine
199  */
200 void do_umount(const char *mntpath, int cookie)
201 {
202 #if DO_DIRECT_MOUNT
203     int loop_fd = cookie;
204
205     if (umount2(mntpath, 0))
206         die("could not umount path");
207
208     if (loop_fd != -1) {
209         ioctl(loop_fd, LOOP_CLR_FD, 0); /* Free loop device */
210         close(loop_fd);
211         loop_fd = -1;
212     }
213 #else
214     pid_t f = fork();
215     pid_t w;
216     int status;
217     (void)cookie;
218
219     if (f < 0) {
220         perror("fork");
221         exit(1);
222     } else if (f == 0) {
223         execl(_PATH_UMOUNT, _PATH_UMOUNT, mntpath, NULL);
224     }
225
226     w = waitpid(f, &status, 0);
227     if (w != f || status) {
228         exit(1);
229     }
230 #endif
231 }
232
233 /*
234  * Make any user-specified ADV modifications
235  */
236 int modify_adv(void)
237 {
238     int rv = 0;
239
240     if (opt.set_once) {
241         if (syslinux_setadv(ADV_BOOTONCE, strlen(opt.set_once), opt.set_once)) {
242             fprintf(stderr, "%s: not enough space for boot-once command\n",
243                     program);
244             rv = -1;
245         }
246     }
247
248     return rv;
249 }
250
251 /*
252  * Modify the ADV of an existing installation
253  */
254 int modify_existing_adv(const char *path)
255 {
256     if (opt.reset_adv)
257         syslinux_reset_adv(syslinux_adv);
258     else if (read_adv(path, "ldlinux.sys") < 0)
259         return 1;
260
261     if (modify_adv() < 0)
262         return 1;
263
264     if (write_adv(path, "ldlinux.sys") < 0)
265         return 1;
266
267     return 0;
268 }
269
270 int main(int argc, char *argv[])
271 {
272     static unsigned char sectbuf[SECTOR_SIZE];
273     int dev_fd, fd;
274     struct stat st;
275     int err = 0;
276     char mntname[128];
277     char *ldlinux_name;
278     char *ldlinux_path;
279     const char *subdir;
280     uint32_t *sectors = NULL;
281     int ldlinux_sectors = (boot_image_len + SECTOR_SIZE - 1) >> SECTOR_SHIFT;
282     const char *errmsg;
283     int mnt_cookie;
284     int patch_sectors;
285     int i;
286
287     mypid = getpid();
288     umask(077);
289     parse_options(argc, argv, MODE_SYSLINUX);
290
291     subdir = opt.directory;
292
293     if (!opt.device)
294         usage(EX_USAGE, MODE_SYSLINUX);
295
296     /*
297      * First make sure we can open the device at all, and that we have
298      * read/write permission.
299      */
300     dev_fd = open(opt.device, O_RDWR);
301     if (dev_fd < 0 || fstat(dev_fd, &st) < 0) {
302         perror(opt.device);
303         exit(1);
304     }
305
306     if (!S_ISBLK(st.st_mode) && !S_ISREG(st.st_mode) && !S_ISCHR(st.st_mode)) {
307         die("not a device or regular file");
308     }
309
310     if (opt.offset && S_ISBLK(st.st_mode)) {
311         die("can't combine an offset with a block device");
312     }
313
314     fs_type = VFAT;
315     xpread(dev_fd, sectbuf, SECTOR_SIZE, opt.offset);
316     fsync(dev_fd);
317
318     /*
319      * Check to see that what we got was indeed an MS-DOS boot sector/superblock
320      */
321     if ((errmsg = syslinux_check_bootsect(sectbuf))) {
322         fprintf(stderr, "%s: %s\n", opt.device, errmsg);
323         exit(1);
324     }
325
326     /*
327      * Now mount the device.
328      */
329     if (geteuid()) {
330         die("This program needs root privilege");
331     } else {
332         int i = 0;
333         struct stat dst;
334         int rv;
335
336         /* We're root or at least setuid.
337            Make a temp dir and pass all the gunky options to mount. */
338
339         if (chdir(_PATH_TMP)) {
340             perror(program);
341             exit(1);
342         }
343 #define TMP_MODE (S_IXUSR|S_IWUSR|S_IXGRP|S_IWGRP|S_IWOTH|S_IXOTH|S_ISVTX)
344
345         if (stat(".", &dst) || !S_ISDIR(dst.st_mode) ||
346             (dst.st_mode & TMP_MODE) != TMP_MODE) {
347             die("possibly unsafe " _PATH_TMP " permissions");
348         }
349
350         for (i = 0;; i++) {
351             snprintf(mntname, sizeof mntname, "syslinux.mnt.%lu.%d",
352                      (unsigned long)mypid, i);
353
354             if (lstat(mntname, &dst) != -1 || errno != ENOENT)
355                 continue;
356
357             rv = mkdir(mntname, 0000);
358
359             if (rv == -1) {
360                 if (errno == EEXIST || errno == EINTR)
361                     continue;
362                 perror(program);
363                 exit(1);
364             }
365
366             if (lstat(mntname, &dst) || dst.st_mode != (S_IFDIR | 0000) ||
367                 dst.st_uid != 0) {
368                 die("someone is trying to symlink race us!");
369             }
370             break;              /* OK, got something... */
371         }
372
373         mntpath = mntname;
374     }
375
376     if (do_mount(dev_fd, &mnt_cookie, mntpath, "vfat") &&
377         do_mount(dev_fd, &mnt_cookie, mntpath, "msdos")) {
378         rmdir(mntpath);
379         die("mount failed");
380     }
381
382     ldlinux_path = alloca(strlen(mntpath) +  (subdir ? strlen(subdir) + 2 : 0));
383     sprintf(ldlinux_path, "%s%s%s",
384             mntpath, subdir ? "//" : "", subdir ? subdir : "");
385
386     ldlinux_name = alloca(strlen(ldlinux_path) + 14);
387     if (!ldlinux_name) {
388         perror(program);
389         err = 1;
390         goto umount;
391     }
392     sprintf(ldlinux_name, "%s//ldlinux.sys", ldlinux_path);
393
394     /* update ADV only ? */
395     if (opt.update_only == -1) {
396         if (opt.reset_adv || opt.set_once) {
397             modify_existing_adv(ldlinux_path);
398             do_umount(mntpath, mnt_cookie);
399             sync();
400             rmdir(mntpath);
401             exit(0);
402         } else {
403             fprintf(stderr, "%s: please specify --install or --update for the future\n", argv[0]);
404             opt.update_only = 0;
405         }
406     }
407
408     /* Read a pre-existing ADV, if already installed */
409     if (opt.reset_adv)
410         syslinux_reset_adv(syslinux_adv);
411     else if (read_adv(ldlinux_path, "ldlinux.sys") < 0)
412         syslinux_reset_adv(syslinux_adv);
413     if (modify_adv() < 0)
414         exit(1);
415
416     if ((fd = open(ldlinux_name, O_RDONLY)) >= 0) {
417         uint32_t zero_attr = 0;
418         ioctl(fd, FAT_IOCTL_SET_ATTRIBUTES, &zero_attr);
419         close(fd);
420     }
421
422     unlink(ldlinux_name);
423     fd = open(ldlinux_name, O_WRONLY | O_CREAT | O_TRUNC, 0444);
424     if (fd < 0) {
425         perror(opt.device);
426         err = 1;
427         goto umount;
428     }
429
430     /* Write it the first time */
431     if (xpwrite(fd, boot_image, boot_image_len, 0) != (int)boot_image_len ||
432         xpwrite(fd, syslinux_adv, 2 * ADV_SIZE,
433                 boot_image_len) != 2 * ADV_SIZE) {
434         fprintf(stderr, "%s: write failure on %s\n", program, ldlinux_name);
435         exit(1);
436     }
437
438     fsync(fd);
439     /*
440      * Set the attributes
441      */
442     {
443         uint32_t attr = 0x07;   /* Hidden+System+Readonly */
444         ioctl(fd, FAT_IOCTL_SET_ATTRIBUTES, &attr);
445     }
446
447     /*
448      * Create a block map.
449      */
450     ldlinux_sectors += 2; /* 2 ADV sectors */
451     sectors = calloc(ldlinux_sectors, sizeof *sectors);
452     if (sectmap(fd, sectors, ldlinux_sectors)) {
453         perror("bmap");
454         exit(1);
455     }
456     close(fd);
457     sync();
458
459 umount:
460     do_umount(mntpath, mnt_cookie);
461     sync();
462     rmdir(mntpath);
463
464     if (err)
465         exit(err);
466
467     /*
468      * Patch ldlinux.sys and the boot sector
469      */
470     i = syslinux_patch(sectors, ldlinux_sectors, opt.stupid_mode, opt.raid_mode, subdir);
471     patch_sectors = (i + SECTOR_SIZE - 1) >> SECTOR_SHIFT;
472
473     /*
474      * Write the now-patched first sectors of ldlinux.sys
475      */
476     for (i = 0; i < patch_sectors; i++) {
477         xpwrite(dev_fd, boot_image + i * SECTOR_SIZE, SECTOR_SIZE,
478                 opt.offset + ((off_t) sectors[i] << SECTOR_SHIFT));
479     }
480
481     /*
482      * To finish up, write the boot sector
483      */
484
485     /* Read the superblock again since it might have changed while mounted */
486     xpread(dev_fd, sectbuf, SECTOR_SIZE, opt.offset);
487
488     /* Copy the syslinux code into the boot sector */
489     syslinux_make_bootsect(sectbuf);
490
491     /* Write new boot sector */
492     xpwrite(dev_fd, sectbuf, SECTOR_SIZE, opt.offset);
493
494     close(dev_fd);
495     sync();
496
497     /* Done! */
498
499     return 0;
500 }