root/mtools/syslinux.c

Revision b0ac906b283b428ba1c7f35fe1e71a84b3d3d9c6, 8.8 KB (checked in by Paulo Alcantara <pcacjr@…>, 9 months ago)

Add NTFS filesystem support to Linux and Windows installers

Signed-off-by: Paulo Alcantara <pcacjr@…>

  • Property mode set to 100755
Line 
1/* ----------------------------------------------------------------------- *
2 *
3 *   Copyright 1998-2008 H. Peter Anvin - All Rights Reserved
4 *   Copyright 2010 Intel Corporation; author: H. Peter Anvin
5 *
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.
11 *
12 * ----------------------------------------------------------------------- */
13
14/*
15 * syslinux.c - Linux installer program for SYSLINUX
16 *
17 * This program now requires mtools.  It turned out to be a lot
18 * easier to deal with than dealing with needing mount privileges.
19 * We need device write permission anyway.
20 */
21
22#define _GNU_SOURCE
23#include <alloca.h>
24#include <errno.h>
25#include <fcntl.h>
26#include <getopt.h>
27#include <inttypes.h>
28#include <mntent.h>
29#include <paths.h>
30#include <stdio.h>
31#include <string.h>
32#include <stdlib.h>
33#include <sysexits.h>
34#include <syslog.h>
35#include <unistd.h>
36#include <sys/types.h>
37#include <sys/stat.h>
38#include <sys/wait.h>
39
40#include "syslinux.h"
41#include "libfat.h"
42#include "setadv.h"
43#include "syslxopt.h"
44#include "syslxfs.h"
45
46char *program;                  /* Name of program */
47pid_t mypid;
48
49void __attribute__ ((noreturn)) die(const char *msg)
50{
51    fprintf(stderr, "%s: %s\n", program, msg);
52    exit(1);
53}
54
55void __attribute__ ((noreturn)) die_err(const char *msg)
56{
57    fprintf(stderr, "%s: %s: %s\n", program, msg, strerror(errno));
58    exit(1);
59}
60
61/*
62 * read/write wrapper functions
63 */
64ssize_t xpread(int fd, void *buf, size_t count, off_t offset)
65{
66    char *bufp = (char *)buf;
67    ssize_t rv;
68    ssize_t done = 0;
69
70    while (count) {
71        rv = pread(fd, bufp, count, offset);
72        if (rv == 0) {
73            die("short read");
74        } else if (rv == -1) {
75            if (errno == EINTR) {
76                continue;
77            } else {
78                die(strerror(errno));
79            }
80        } else {
81            bufp += rv;
82            offset += rv;
83            done += rv;
84            count -= rv;
85        }
86    }
87
88    return done;
89}
90
91ssize_t xpwrite(int fd, const void *buf, size_t count, off_t offset)
92{
93    const char *bufp = (const char *)buf;
94    ssize_t rv;
95    ssize_t done = 0;
96
97    while (count) {
98        rv = pwrite(fd, bufp, count, offset);
99        if (rv == 0) {
100            die("short write");
101        } else if (rv == -1) {
102            if (errno == EINTR) {
103                continue;
104            } else {
105                die(strerror(errno));
106            }
107        } else {
108            bufp += rv;
109            offset += rv;
110            done += rv;
111            count -= rv;
112        }
113    }
114
115    return done;
116}
117
118/*
119 * Version of the read function suitable for libfat
120 */
121int libfat_xpread(intptr_t pp, void *buf, size_t secsize,
122                  libfat_sector_t sector)
123{
124    off_t offset = (off_t) sector * secsize + opt.offset;
125    return xpread(pp, buf, secsize, offset);
126}
127
128int main(int argc, char *argv[])
129{
130    static unsigned char sectbuf[SECTOR_SIZE];
131    int dev_fd;
132    struct stat st;
133    int status;
134    const char *tmpdir;
135    char *mtools_conf;
136    int mtc_fd;
137    FILE *mtc, *mtp;
138    struct libfat_filesystem *fs;
139    libfat_sector_t s, *secp;
140    libfat_sector_t *sectors;
141    int32_t ldlinux_cluster;
142    int nsectors;
143    const char *errmsg;
144    int ldlinux_sectors, patch_sectors;
145    int i;
146
147    (void)argc;                 /* Unused */
148
149    mypid = getpid();
150    program = argv[0];
151
152    parse_options(argc, argv, MODE_SYSLINUX);
153
154    if (!opt.device)
155        usage(EX_USAGE, MODE_SYSLINUX);
156
157    if (opt.sectors || opt.heads || opt.reset_adv || opt.set_once
158        || (opt.update_only > 0) || opt.menu_save) {
159        fprintf(stderr,
160                "At least one specified option not yet implemented"
161                " for this installer.\n");
162        exit(1);
163    }
164
165    /*
166     * Temp directory of choice...
167     */
168    tmpdir = getenv("TMPDIR");
169    if (!tmpdir) {
170#ifdef P_tmpdir
171        tmpdir = P_tmpdir;
172#elif defined(_PATH_TMP)
173        tmpdir = _PATH_TMP;
174#else
175        tmpdir = "/tmp";
176#endif
177    }
178
179    /*
180     * First make sure we can open the device at all, and that we have
181     * read/write permission.
182     */
183    dev_fd = open(opt.device, O_RDWR);
184    if (dev_fd < 0 || fstat(dev_fd, &st) < 0) {
185        die_err(opt.device);
186        exit(1);
187    }
188
189    if (!opt.force && !S_ISBLK(st.st_mode) && !S_ISREG(st.st_mode)) {
190        fprintf(stderr,
191                "%s: not a block device or regular file (use -f to override)\n",
192                opt.device);
193        exit(1);
194    }
195
196    xpread(dev_fd, sectbuf, SECTOR_SIZE, opt.offset);
197
198    /*
199     * Check to see that what we got was indeed an MS-DOS boot sector/superblock
200     */
201    if ((errmsg = syslinux_check_bootsect(sectbuf, NULL))) {
202        die(errmsg);
203    }
204
205    /*
206     * Create an mtools configuration file
207     */
208    if (asprintf(&mtools_conf, "%s//syslinux-mtools-XXXXXX", tmpdir) < 0 ||
209        !mtools_conf)
210        die_err(tmpdir);
211
212    mtc_fd = mkstemp(mtools_conf);
213    if (mtc_fd < 0 || !(mtc = fdopen(mtc_fd, "w")))
214        die_err(mtools_conf);
215
216    fprintf(mtc,
217            /* These are needed for some flash memories */
218            "MTOOLS_SKIP_CHECK=1\n"
219            "MTOOLS_FAT_COMPATIBILITY=1\n"
220            "drive s:\n"
221            "  file=\"/proc/%lu/fd/%d\"\n"
222            "  offset=%llu\n",
223            (unsigned long)mypid,
224            dev_fd, (unsigned long long)opt.offset);
225
226    if (ferror(mtc) || fclose(mtc))
227        die_err(mtools_conf);
228
229    /*
230     * Run mtools to create the LDLINUX.SYS file
231     */
232    if (setenv("MTOOLSRC", mtools_conf, 1)) {
233        perror(program);
234        exit(1);
235    }
236
237    /*
238     * Create a vacuous ADV in memory.  This should be smarter.
239     */
240    syslinux_reset_adv(syslinux_adv);
241
242    /* This command may fail legitimately */
243    status = system("mattrib -h -r -s s:/ldlinux.sys 2>/dev/null");
244    (void)status;               /* Keep _FORTIFY_SOURCE happy */
245
246    mtp = popen("mcopy -D o -D O -o - s:/ldlinux.sys", "w");
247    if (!mtp ||
248        fwrite(syslinux_ldlinux, 1, syslinux_ldlinux_len, mtp)
249                != syslinux_ldlinux_len ||
250        fwrite(syslinux_adv, 1, 2 * ADV_SIZE, mtp)
251                != 2 * ADV_SIZE ||
252        (status = pclose(mtp), !WIFEXITED(status) || WEXITSTATUS(status))) {
253        die("failed to create ldlinux.sys");
254    }
255
256    /*
257     * Now, use libfat to create a block map
258     */
259    ldlinux_sectors = (syslinux_ldlinux_len + 2 * ADV_SIZE
260                       + SECTOR_SIZE - 1) >> SECTOR_SHIFT;
261    sectors = calloc(ldlinux_sectors, sizeof *sectors);
262    fs = libfat_open(libfat_xpread, dev_fd);
263    ldlinux_cluster = libfat_searchdir(fs, 0, "LDLINUX SYS", NULL);
264    secp = sectors;
265    nsectors = 0;
266    s = libfat_clustertosector(fs, ldlinux_cluster);
267    while (s && nsectors < ldlinux_sectors) {
268        *secp++ = s;
269        nsectors++;
270        s = libfat_nextsector(fs, s);
271    }
272    libfat_close(fs);
273
274    /* Patch ldlinux.sys and the boot sector */
275    i = syslinux_patch(sectors, nsectors, opt.stupid_mode, opt.raid_mode,
276                       opt.directory, NULL);
277    patch_sectors = (i + SECTOR_SIZE - 1) >> SECTOR_SHIFT;
278
279    /* Write the now-patched first sectors of ldlinux.sys */
280    for (i = 0; i < patch_sectors; i++) {
281        xpwrite(dev_fd, syslinux_ldlinux + i * SECTOR_SIZE, SECTOR_SIZE,
282                opt.offset + ((off_t) sectors[i] << SECTOR_SHIFT));
283    }
284
285    /* Move ldlinux.sys to the desired location */
286    if (opt.directory) {
287        char target_file[4096], command[5120];
288        char *cp = target_file, *ep = target_file + sizeof target_file - 16;
289        const char *sd;
290        int slash = 1;
291
292        cp += sprintf(cp, "'s:/");
293        for (sd = opt.directory; *sd; sd++) {
294            if (*sd == '/' || *sd == '\\') {
295                if (slash)
296                    continue;   /* Remove duplicated slashes */
297                slash = 1;
298            } else if (*sd == '\'' || *sd == '!') {
299                slash = 0;
300                if (cp < ep)
301                    *cp++ = '\'';
302                if (cp < ep)
303                    *cp++ = '\\';
304                if (cp < ep)
305                    *cp++ = *sd;
306                if (cp < ep)
307                    *cp++ = '\'';
308                continue;
309            } else {
310                slash = 0;
311            }
312
313            if (cp < ep)
314                *cp++ = *sd;
315        }
316        if (!slash)
317            *cp++ = '/';
318        strcpy(cp, "ldlinux.sys'");
319
320        /* This command may fail legitimately */
321        sprintf(command, "mattrib -h -r -s %s 2>/dev/null", target_file);
322        status = system(command);
323        (void)status;           /* Keep _FORTIFY_SOURCE happy */
324
325        sprintf(command, "mmove -D o -D O s:/ldlinux.sys %s", target_file);
326        status = system(command);
327
328        if (!WIFEXITED(status) || WEXITSTATUS(status)) {
329            fprintf(stderr,
330                    "%s: warning: unable to move ldlinux.sys\n", program);
331
332            status = system("mattrib +r +h +s s:/ldlinux.sys");
333        } else {
334            sprintf(command, "mattrib +r +h +s %s", target_file);
335            status = system(command);
336        }
337    } else {
338        status = system("mattrib +r +h +s s:/ldlinux.sys");
339    }
340
341    if (!WIFEXITED(status) || WEXITSTATUS(status)) {
342        fprintf(stderr,
343                "%s: warning: failed to set system bit on ldlinux.sys\n",
344                program);
345    }
346
347    /*
348     * Cleanup
349     */
350    unlink(mtools_conf);
351
352    /*
353     * To finish up, write the boot sector
354     */
355
356    /* Read the superblock again since it might have changed while mounted */
357    xpread(dev_fd, sectbuf, SECTOR_SIZE, opt.offset);
358
359    /* Copy the syslinux code into the boot sector */
360    syslinux_make_bootsect(sectbuf, VFAT);
361
362    /* Write new boot sector */
363    xpwrite(dev_fd, sectbuf, SECTOR_SIZE, opt.offset);
364
365    close(dev_fd);
366    sync();
367
368    /* Done! */
369
370    return 0;
371}
Note: See TracBrowser for help on using the browser.