diff options
author | Feng Tang <feng.tang@intel.com> | 2010-07-14 11:08:06 +0800 |
---|---|---|
committer | Feng Tang <feng.tang@intel.com> | 2010-07-20 11:10:04 +0800 |
commit | ff6ef23ed4e8459dbe6e97d0a016e8f89f5da258 (patch) | |
tree | 05dfeec6bf56aff95159d4e9dde2dbd267dc1a2a /libinstaller | |
parent | 3400a8754214f4cd69d49d85c0396665d7a6415f (diff) | |
download | syslinux-ff6ef23ed4e8459dbe6e97d0a016e8f89f5da258.tar.gz |
elflink: add new files
new file: com32/include/getopt.h
new file: com32/include/ilog2.h
new file: com32/lib/getopt_long.c
new file: com32/lib/sys/sleep.c
new file: com32/sysdump/acpi.c
new file: com32/sysdump/be_srec.c
new file: com32/sysdump/rbtree.c
new file: com32/sysdump/rbtree.h
new file: dos/getsetsl.c
new file: dos/memmove.S
new file: libinstaller/advio.c
new file: libinstaller/fat.c
new file: libinstaller/linuxioctl.h
new file: memdisk/compiler.h
new file: memdisk/mstructs.h
new file: utils/memdiskfind.c
Diffstat (limited to 'libinstaller')
-rw-r--r-- | libinstaller/advio.c | 162 | ||||
-rw-r--r-- | libinstaller/fat.c | 129 | ||||
-rw-r--r-- | libinstaller/linuxioctl.h | 47 |
3 files changed, 338 insertions, 0 deletions
diff --git a/libinstaller/advio.c b/libinstaller/advio.c new file mode 100644 index 00000000..56f607d0 --- /dev/null +++ b/libinstaller/advio.c @@ -0,0 +1,162 @@ +/* ----------------------------------------------------------------------- * + * + * Copyright 2007-2008 H. Peter Anvin - All Rights Reserved + * Copyright 2010 Intel Corporation; author: H. Peter Anvin + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, Inc., 53 Temple Place Ste 330, + * Boston MA 02111-1307, USA; either version 2 of the License, or + * (at your option) any later version; incorporated herein by reference. + * + * ----------------------------------------------------------------------- */ + +/* + * advio.c + * + * Linux ADV I/O + * + * Return 0 on success, -1 on error, and set errno. + * + */ +#define _GNU_SOURCE + +#include <stdio.h> +#include <stdlib.h> +#include <stddef.h> +#include <stdint.h> +#include <string.h> +#include <unistd.h> +#include <errno.h> +#include <fcntl.h> +#include <sys/stat.h> +#include <sys/types.h> +#include "syslxint.h" +#include "syslxcom.h" + +/* + * Read the ADV from an existing instance, or initialize if invalid. + * Returns -1 on fatal errors, 0 if ADV is okay, 1 if the ADV is + * invalid, and 2 if the file does not exist. + */ +int read_adv(const char *path, const char *cfg) +{ + char *file; + int fd = -1; + struct stat st; + int err = 0; + int rv; + + rv = asprintf(&file, "%s%s%s", path, + path[0] && path[strlen(path) - 1] == '/' ? "" : "/", cfg); + + if (rv < 0 || !file) { + perror(program); + return -1; + } + + fd = open(file, O_RDONLY); + if (fd < 0) { + if (errno != ENOENT) { + err = -1; + } else { + syslinux_reset_adv(syslinux_adv); + err = 2; /* Nonexistence is not a fatal error */ + } + } else if (fstat(fd, &st)) { + err = -1; + } else if (st.st_size < 2 * ADV_SIZE) { + /* Too small to be useful */ + syslinux_reset_adv(syslinux_adv); + err = 0; /* Nothing to read... */ + } else if (xpread(fd, syslinux_adv, 2 * ADV_SIZE, + st.st_size - 2 * ADV_SIZE) != 2 * ADV_SIZE) { + err = -1; + } else { + /* We got it... maybe? */ + err = syslinux_validate_adv(syslinux_adv) ? 1 : 0; + } + + if (err < 0) + perror(file); + + if (fd >= 0) + close(fd); + + free(file); + + return err; +} + +/* + * Update the ADV in an existing installation. + */ +int write_adv(const char *path, const char *cfg) +{ + unsigned char advtmp[2 * ADV_SIZE]; + char *file; + int fd = -1; + struct stat st, xst; + int err = 0; + int rv; + + rv = asprintf(&file, "%s%s%s", path, + path[0] && path[strlen(path) - 1] == '/' ? "" : "/", cfg); + + if (rv < 0 || !file) { + perror(program); + return -1; + } + + fd = open(file, O_RDONLY); + if (fd < 0) { + err = -1; + } else if (fstat(fd, &st)) { + err = -1; + } else if (st.st_size < 2 * ADV_SIZE) { + /* Too small to be useful */ + err = -2; + } else if (xpread(fd, advtmp, 2 * ADV_SIZE, + st.st_size - 2 * ADV_SIZE) != 2 * ADV_SIZE) { + err = -1; + } else { + /* We got it... maybe? */ + err = syslinux_validate_adv(advtmp) ? -2 : 0; + if (!err) { + /* Got a good one, write our own ADV here */ + clear_attributes(fd); + + /* Need to re-open read-write */ + close(fd); + fd = open(file, O_RDWR | O_SYNC); + if (fd < 0) { + err = -1; + } else if (fstat(fd, &xst) || xst.st_ino != st.st_ino || + xst.st_dev != st.st_dev || xst.st_size != st.st_size) { + fprintf(stderr, "%s: race condition on write\n", file); + err = -2; + } + /* Write our own version ... */ + if (xpwrite(fd, syslinux_adv, 2 * ADV_SIZE, + st.st_size - 2 * ADV_SIZE) != 2 * ADV_SIZE) { + err = -1; + } + + sync(); + set_attributes(fd); + } + } + + if (err == -2) + fprintf(stderr, "%s: cannot write auxilliary data (need --update)?\n", + file); + else if (err == -1) + perror(file); + + if (fd >= 0) + close(fd); + if (file) + free(file); + + return err; +} diff --git a/libinstaller/fat.c b/libinstaller/fat.c new file mode 100644 index 00000000..e2101353 --- /dev/null +++ b/libinstaller/fat.c @@ -0,0 +1,129 @@ +/* ----------------------------------------------------------------------- * + * + * Copyright 1998-2008 H. Peter Anvin - All Rights Reserved + * Copyright 2009-2010 Intel Corporation; author H. Peter Anvin + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, Inc., 53 Temple Place Ste 330, + * Boston MA 02111-1307, USA; either version 2 of the License, or + * (at your option) any later version; incorporated herein by reference. + * + * ----------------------------------------------------------------------- */ + +/* + * fat.c - Initial sanity check for FAT-based installers + */ + +#define _XOPEN_SOURCE 500 /* Required on glibc 2.x */ +#define _BSD_SOURCE +#include <stdio.h> +#include <inttypes.h> +#include <string.h> +#include <stddef.h> +#include <stdlib.h> + +#include "syslinux.h" +#include "syslxint.h" + +void syslinux_make_bootsect(void *bs) +{ + struct boot_sector *bootsect = bs; + const struct boot_sector *sbs = + (const struct boot_sector *)boot_sector; + + memcpy(&bootsect->bsHead, &sbs->bsHead, bsHeadLen); + memcpy(&bootsect->bsCode, &sbs->bsCode, bsCodeLen); +} + +/* + * Check to see that what we got was indeed an MS-DOS boot sector/superblock; + * Return NULL if OK and otherwise an error message; + */ +const char *syslinux_check_bootsect(const void *bs) +{ + int veryold; + int sectorsize; + long long sectors, fatsectors, dsectors; + long long clusters; + int rootdirents, clustersize; + const struct boot_sector *sectbuf = bs; + + veryold = 0; + + /* Must be 0xF0 or 0xF8..0xFF */ + if (get_8(§buf->bsMedia) != 0xF0 && get_8(§buf->bsMedia) < 0xF8) + return "invalid media signature (not a FAT filesystem?)"; + + sectorsize = get_16(§buf->bsBytesPerSec); + if (sectorsize == SECTOR_SIZE) + ; /* ok */ + else if (sectorsize >= 512 && sectorsize <= 4096 && + (sectorsize & (sectorsize - 1)) == 0) + return "unsupported sectors size"; + else + return "impossible sector size"; + + clustersize = get_8(§buf->bsSecPerClust); + if (clustersize == 0 || (clustersize & (clustersize - 1))) + return "impossible cluster size"; + + sectors = get_16(§buf->bsSectors); + sectors = sectors ? sectors : get_32(§buf->bsHugeSectors); + + dsectors = sectors - get_16(§buf->bsResSectors); + + fatsectors = get_16(§buf->bsFATsecs); + fatsectors = fatsectors ? fatsectors : get_32(§buf->bs32.FATSz32); + fatsectors *= get_8(§buf->bsFATs); + dsectors -= fatsectors; + + rootdirents = get_16(§buf->bsRootDirEnts); + dsectors -= (rootdirents + sectorsize / 32 - 1) / sectorsize; + + if (dsectors < 0) + return "negative number of data sectors"; + + if (fatsectors == 0) + return "zero FAT sectors"; + + clusters = dsectors / clustersize; + + if (clusters < 0xFFF5) { + /* FAT12 or FAT16 */ + + if (!get_16(§buf->bsFATsecs)) + return "zero FAT sectors (FAT12/16)"; + + if (get_8(§buf->bs16.BootSignature) == 0x29) { + if (!memcmp(§buf->bs16.FileSysType, "FAT12 ", 8)) { + if (clusters >= 0xFF5) + return "more than 4084 clusters but claims FAT12"; + } else if (!memcmp(§buf->bs16.FileSysType, "FAT16 ", 8)) { + if (clusters < 0xFF5) + return "less than 4084 clusters but claims FAT16"; + } else if (!memcmp(§buf->bs16.FileSysType, "FAT32 ", 8)) { + return "less than 65525 clusters but claims FAT32"; + } else if (memcmp(§buf->bs16.FileSysType, "FAT ", 8)) { + static char fserr[] = + "filesystem type \"????????\" not supported"; + memcpy(fserr + 17, §buf->bs16.FileSysType, 8); + return fserr; + } + } + } else if (clusters < 0x0FFFFFF5) { + /* + * FAT32... + * + * Moving the FileSysType and BootSignature was a lovely stroke + * of M$ idiocy... + */ + if (get_8(§buf->bs32.BootSignature) != 0x29 || + memcmp(§buf->bs32.FileSysType, "FAT32 ", 8)) + return "missing FAT32 signature"; + } else { + return "impossibly large number of clusters"; + } + + return NULL; +} diff --git a/libinstaller/linuxioctl.h b/libinstaller/linuxioctl.h new file mode 100644 index 00000000..7ef919a3 --- /dev/null +++ b/libinstaller/linuxioctl.h @@ -0,0 +1,47 @@ +/* + * linuxioctl.h + * + * Wrapper for Linux ioctl definitions, including workarounds + */ + +#ifndef LIBINSTALLER_LINUXIOCTL_H +#define LIBINSTALLER_LINUXIOCTL_H + +#include <sys/ioctl.h> + +#define statfs _kernel_statfs /* HACK to deal with broken 2.4 distros */ + +#include <linux/fd.h> /* Floppy geometry */ +#include <linux/hdreg.h> /* Hard disk geometry */ + +#include <linux/fs.h> /* FIGETBSZ, FIBMAP, FS_IOC_FIEMAP */ +#include <linux/msdos_fs.h> /* FAT_IOCTL_SET_ATTRIBUTES */ + +#undef SECTOR_SIZE /* Defined in msdos_fs.h for no good reason */ +#undef SECTOR_BITS +#include <linux/ext2_fs.h> /* EXT2_IOC_* */ + +#ifndef FAT_IOCTL_GET_ATTRIBUTES +# define FAT_IOCTL_GET_ATTRIBUTES _IOR('r', 0x10, __u32) +#endif + +#ifndef FAT_IOCTL_SET_ATTRIBUTES +# define FAT_IOCTL_SET_ATTRIBUTES _IOW('r', 0x11, __u32) +#endif + +#include <linux/fiemap.h> /* FIEMAP definitions */ + +#ifndef FS_IOC_FIEMAP +# define FS_IOC_FIEMAP _IOWR('f', 11, struct fiemap) +#endif + +#undef statfs + +#if defined(__linux__) && !defined(BLKGETSIZE64) +/* This takes a u64, but the size field says size_t. Someone screwed big. */ +# define BLKGETSIZE64 _IOR(0x12,114,size_t) +#endif + +#include <linux/loop.h> + +#endif /* LIBINSTALLER_LINUXIOCTL_H */ |