diff --git a/usr.bin/mkimg/ebr.c b/usr.bin/mkimg/ebr.c index a069982e7aa3..bee2e64bea14 100644 --- a/usr.bin/mkimg/ebr.c +++ b/usr.bin/mkimg/ebr.c @@ -1,130 +1,131 @@ /*- * Copyright (c) 2014 Juniper Networks, Inc. * All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * 1. Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * 2. Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in the * documentation and/or other materials provided with the distribution. * * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * SUCH DAMAGE. */ #include __FBSDID("$FreeBSD$"); #include #include #include #include #include #include "endian.h" #include "image.h" #include "mkimg.h" #include "scheme.h" static struct mkimg_alias ebr_aliases[] = { { ALIAS_FAT16B, ALIAS_INT2TYPE(DOSPTYP_FAT16) }, { ALIAS_FAT32, ALIAS_INT2TYPE(DOSPTYP_FAT32) }, + { ALIAS_FAT32LBA, ALIAS_INT2TYPE(DOSPTYP_FAT32LBA) }, { ALIAS_FREEBSD, ALIAS_INT2TYPE(DOSPTYP_386BSD) }, { ALIAS_NONE, 0 } }; static lba_t ebr_metadata(u_int where, lba_t blk) { blk += (where == SCHEME_META_PART_BEFORE) ? 1 : 0; return (round_track(blk)); } static void ebr_chs(u_char *cylp, u_char *hdp, u_char *secp, lba_t lba) { u_int cyl, hd, sec; mkimg_chs(lba, 1023, &cyl, &hd, &sec); *cylp = cyl; *hdp = hd; *secp = (sec & 0x3f) | ((cyl >> 2) & 0xc0); } static int ebr_write(lba_t imgsz __unused, void *bootcode __unused) { u_char *ebr; struct dos_partition *dp; struct part *part, *next; lba_t block, size; int error; ebr = malloc(secsz); if (ebr == NULL) return (ENOMEM); memset(ebr, 0, secsz); le16enc(ebr + DOSMAGICOFFSET, DOSMAGIC); error = 0; TAILQ_FOREACH(part, &partlist, link) { block = part->block - nsecs; size = round_track(part->size); dp = (void *)(ebr + DOSPARTOFF); ebr_chs(&dp->dp_scyl, &dp->dp_shd, &dp->dp_ssect, nsecs); dp->dp_typ = ALIAS_TYPE2INT(part->type); ebr_chs(&dp->dp_ecyl, &dp->dp_ehd, &dp->dp_esect, part->block + size - 1); le32enc(&dp->dp_start, nsecs); le32enc(&dp->dp_size, size); /* Add link entry */ next = TAILQ_NEXT(part, link); if (next != NULL) { size = round_track(next->size); dp++; ebr_chs(&dp->dp_scyl, &dp->dp_shd, &dp->dp_ssect, next->block - nsecs); dp->dp_typ = DOSPTYP_EXT; ebr_chs(&dp->dp_ecyl, &dp->dp_ehd, &dp->dp_esect, next->block + size - 1); le32enc(&dp->dp_start, next->block - nsecs); le32enc(&dp->dp_size, size + nsecs); } error = image_write(block, ebr, 1); if (error) break; memset(ebr + DOSPARTOFF, 0, 2 * DOSPARTSIZE); } free(ebr); return (error); } static struct mkimg_scheme ebr_scheme = { .name = "ebr", .description = "Extended Boot Record", .aliases = ebr_aliases, .metadata = ebr_metadata, .write = ebr_write, .nparts = 4096, .maxsecsz = 4096 }; SCHEME_DEFINE(ebr_scheme); diff --git a/usr.bin/mkimg/mbr.c b/usr.bin/mkimg/mbr.c index 46767ea96d06..e52e9df7ca64 100644 --- a/usr.bin/mkimg/mbr.c +++ b/usr.bin/mkimg/mbr.c @@ -1,124 +1,125 @@ /*- * Copyright (c) 2014 Juniper Networks, Inc. * All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * 1. Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * 2. Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in the * documentation and/or other materials provided with the distribution. * * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * SUCH DAMAGE. */ #include __FBSDID("$FreeBSD$"); #include #include #include #include #include #include "endian.h" #include "image.h" #include "mkimg.h" #include "scheme.h" static struct mkimg_alias mbr_aliases[] = { { ALIAS_EBR, ALIAS_INT2TYPE(DOSPTYP_EXT) }, { ALIAS_EFI, ALIAS_INT2TYPE(DOSPTYP_EFI) }, { ALIAS_FAT16B, ALIAS_INT2TYPE(DOSPTYP_FAT16) }, { ALIAS_FAT32, ALIAS_INT2TYPE(DOSPTYP_FAT32) }, + { ALIAS_FAT32LBA, ALIAS_INT2TYPE(DOSPTYP_FAT32LBA) }, { ALIAS_FREEBSD, ALIAS_INT2TYPE(DOSPTYP_386BSD) }, { ALIAS_NTFS, ALIAS_INT2TYPE(DOSPTYP_NTFS) }, { ALIAS_PPCBOOT, ALIAS_INT2TYPE(DOSPTYP_PPCBOOT) }, { ALIAS_NONE, 0 } /* Keep last! */ }; static lba_t mbr_metadata(u_int where, lba_t blk) { blk += (where == SCHEME_META_IMG_START) ? 1 : 0; return (round_track(blk)); } static void mbr_chs(u_char *cylp, u_char *hdp, u_char *secp, lba_t lba) { u_int cyl, hd, sec; mkimg_chs(lba, 1023, &cyl, &hd, &sec); *cylp = cyl; *hdp = hd; *secp = (sec & 0x3f) | ((cyl >> 2) & 0xc0); } static int mbr_write(lba_t imgsz __unused, void *bootcode) { u_char *mbr; struct dos_partition *dpbase, *dp; struct part *part; lba_t size; int error; mbr = malloc(secsz); if (mbr == NULL) return (ENOMEM); if (bootcode != NULL) { memcpy(mbr, bootcode, DOSPARTOFF); memset(mbr + DOSPARTOFF, 0, secsz - DOSPARTOFF); } else memset(mbr, 0, secsz); le16enc(mbr + DOSMAGICOFFSET, DOSMAGIC); dpbase = (void *)(mbr + DOSPARTOFF); TAILQ_FOREACH(part, &partlist, link) { size = round_track(part->size); dp = dpbase + part->index; if (active_partition != 0) dp->dp_flag = (part->index + 1 == active_partition) ? 0x80 : 0; else dp->dp_flag = (part->index == 0 && bootcode != NULL) ? 0x80 : 0; mbr_chs(&dp->dp_scyl, &dp->dp_shd, &dp->dp_ssect, part->block); dp->dp_typ = ALIAS_TYPE2INT(part->type); mbr_chs(&dp->dp_ecyl, &dp->dp_ehd, &dp->dp_esect, part->block + size - 1); le32enc(&dp->dp_start, part->block); le32enc(&dp->dp_size, size); } error = image_write(0, mbr, 1); free(mbr); return (error); } static struct mkimg_scheme mbr_scheme = { .name = "mbr", .description = "Master Boot Record", .aliases = mbr_aliases, .metadata = mbr_metadata, .write = mbr_write, .bootcode = 512, .nparts = NDOSPART, .maxsecsz = 4096 }; SCHEME_DEFINE(mbr_scheme); diff --git a/usr.bin/mkimg/scheme.c b/usr.bin/mkimg/scheme.c index c81182837b2e..5afe3245a2ba 100644 --- a/usr.bin/mkimg/scheme.c +++ b/usr.bin/mkimg/scheme.c @@ -1,200 +1,201 @@ /*- * Copyright (c) 2013,2014 Juniper Networks, Inc. * All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * 1. Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * 2. Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in the * documentation and/or other materials provided with the distribution. * * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * SUCH DAMAGE. */ #include __FBSDID("$FreeBSD$"); #include #include #include #include #include #include #include #include "image.h" #include "mkimg.h" #include "scheme.h" static struct { const char *name; enum alias alias; } scheme_alias[] = { { "ebr", ALIAS_EBR }, { "efi", ALIAS_EFI }, { "fat16b", ALIAS_FAT16B }, { "fat32", ALIAS_FAT32 }, + { "fat32lba", ALIAS_FAT32LBA }, { "freebsd", ALIAS_FREEBSD }, { "freebsd-boot", ALIAS_FREEBSD_BOOT }, { "freebsd-nandfs", ALIAS_FREEBSD_NANDFS }, { "freebsd-swap", ALIAS_FREEBSD_SWAP }, { "freebsd-ufs", ALIAS_FREEBSD_UFS }, { "freebsd-vinum", ALIAS_FREEBSD_VINUM }, { "freebsd-zfs", ALIAS_FREEBSD_ZFS }, { "mbr", ALIAS_MBR }, { "ntfs", ALIAS_NTFS }, { "prepboot", ALIAS_PPCBOOT }, { NULL, ALIAS_NONE } /* Keep last! */ }; static struct mkimg_scheme *first; static struct mkimg_scheme *scheme; static void *bootcode; static enum alias scheme_parse_alias(const char *name) { u_int idx; idx = 0; while (scheme_alias[idx].name != NULL) { if (strcasecmp(scheme_alias[idx].name, name) == 0) return (scheme_alias[idx].alias); idx++; } return (ALIAS_NONE); } struct mkimg_scheme * scheme_iterate(struct mkimg_scheme *s) { return ((s == NULL) ? first : s->next); } void scheme_register(struct mkimg_scheme *s) { s->next = first; first = s; } int scheme_select(const char *spec) { struct mkimg_scheme *s; s = NULL; while ((s = scheme_iterate(s)) != NULL) { if (strcasecmp(spec, s->name) == 0) { scheme = s; return (0); } } return (EINVAL); } struct mkimg_scheme * scheme_selected(void) { return (scheme); } int scheme_bootcode(int fd) { struct stat sb; if (scheme == NULL || scheme->bootcode == 0) return (ENXIO); if (fstat(fd, &sb) == -1) return (errno); if (sb.st_size > scheme->bootcode) return (EFBIG); bootcode = malloc(scheme->bootcode); if (bootcode == NULL) return (ENOMEM); memset(bootcode, 0, scheme->bootcode); if (read(fd, bootcode, sb.st_size) != sb.st_size) { free(bootcode); bootcode = NULL; return (errno); } return (0); } int scheme_check_part(struct part *p) { struct mkimg_alias *iter; enum alias alias; assert(scheme != NULL); /* Check the partition type alias */ alias = scheme_parse_alias(p->alias); if (alias == ALIAS_NONE) return (EINVAL); iter = scheme->aliases; while (iter->alias != ALIAS_NONE) { if (alias == iter->alias) break; iter++; } if (iter->alias == ALIAS_NONE) return (EINVAL); p->type = iter->type; /* Validate the optional label. */ if (p->label != NULL) { if (strlen(p->label) > scheme->labellen) return (EINVAL); } return (0); } u_int scheme_max_parts(void) { return ((scheme == NULL) ? 0 : scheme->nparts); } u_int scheme_max_secsz(void) { return ((scheme == NULL) ? INT_MAX+1U : scheme->maxsecsz); } lba_t scheme_metadata(u_int where, lba_t start) { return ((scheme == NULL) ? start : scheme->metadata(where, start)); } int scheme_write(lba_t end) { return ((scheme == NULL) ? 0 : scheme->write(end, bootcode)); } diff --git a/usr.bin/mkimg/scheme.h b/usr.bin/mkimg/scheme.h index 86b92a372860..00c1b7f516c9 100644 --- a/usr.bin/mkimg/scheme.h +++ b/usr.bin/mkimg/scheme.h @@ -1,96 +1,97 @@ /*- * Copyright (c) 2013,2014 Juniper Networks, Inc. * All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * 1. Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * 2. Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in the * documentation and/or other materials provided with the distribution. * * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * SUCH DAMAGE. * * $FreeBSD$ */ #ifndef _MKIMG_SCHEME_H_ #define _MKIMG_SCHEME_H_ enum alias { ALIAS_NONE, /* Keep first! */ /* start */ ALIAS_EBR, ALIAS_EFI, ALIAS_FAT16B, ALIAS_FAT32, + ALIAS_FAT32LBA, ALIAS_FREEBSD, ALIAS_FREEBSD_BOOT, ALIAS_FREEBSD_NANDFS, ALIAS_FREEBSD_SWAP, ALIAS_FREEBSD_UFS, ALIAS_FREEBSD_VINUM, ALIAS_FREEBSD_ZFS, ALIAS_MBR, ALIAS_NTFS, ALIAS_PPCBOOT, /* end */ ALIAS_COUNT /* Keep last! */ }; struct mkimg_alias { u_int alias; uintptr_t type; #define ALIAS_PTR2TYPE(p) (uintptr_t)(p) #define ALIAS_INT2TYPE(i) (i) #define ALIAS_TYPE2PTR(p) (void *)(p) #define ALIAS_TYPE2INT(i) (i) }; struct mkimg_scheme { struct mkimg_scheme *next; const char *name; const char *description; struct mkimg_alias *aliases; lba_t (*metadata)(u_int, lba_t); #define SCHEME_META_IMG_START 1 #define SCHEME_META_IMG_END 2 #define SCHEME_META_PART_BEFORE 3 #define SCHEME_META_PART_AFTER 4 #define SCHEME_META_PART_ABSOLUTE 5 int (*write)(lba_t, void *); u_int nparts; u_int labellen; u_int bootcode; u_int maxsecsz; }; #define SCHEME_DEFINE(nm) \ static void scheme_register_##nm(void) __attribute__((constructor)); \ static void scheme_register_##nm(void) { scheme_register(&nm); } struct mkimg_scheme *scheme_iterate(struct mkimg_scheme *); void scheme_register(struct mkimg_scheme *); int scheme_select(const char *); struct mkimg_scheme *scheme_selected(void); int scheme_bootcode(int fd); int scheme_check_part(struct part *); u_int scheme_max_parts(void); u_int scheme_max_secsz(void); lba_t scheme_metadata(u_int, lba_t); int scheme_write(lba_t); #endif /* _MKIMG_SCHEME_H_ */