Merge ^/head/usr.bin/mkimg@265546

This commit is contained in:
marcel 2014-05-07 14:20:58 +00:00
parent 30dea1dbd2
commit e22d96c863
9 changed files with 83 additions and 55 deletions

15
apm.c
View File

@ -38,8 +38,13 @@ __FBSDID("$FreeBSD$");
#include "mkimg.h"
#include "scheme.h"
#ifndef APM_ENT_TYPE_FREEBSD_NANDFS
#define APM_ENT_TYPE_FREEBSD_NANDFS "FreeBSD-nandfs"
#endif
static struct mkimg_alias apm_aliases[] = {
{ ALIAS_FREEBSD, ALIAS_PTR2TYPE(APM_ENT_TYPE_FREEBSD) },
{ ALIAS_FREEBSD_BOOT, ALIAS_PTR2TYPE(APM_ENT_TYPE_APPLE_BOOT) },
{ ALIAS_FREEBSD_NANDFS, ALIAS_PTR2TYPE(APM_ENT_TYPE_FREEBSD_NANDFS) },
{ ALIAS_FREEBSD_SWAP, ALIAS_PTR2TYPE(APM_ENT_TYPE_FREEBSD_SWAP) },
{ ALIAS_FREEBSD_UFS, ALIAS_PTR2TYPE(APM_ENT_TYPE_FREEBSD_UFS) },
@ -58,13 +63,12 @@ apm_metadata(u_int where)
}
static int
apm_write(int fd __unused, lba_t imgsz __unused, void *bootcode __unused)
apm_write(int fd, lba_t imgsz, void *bootcode __unused)
{
u_char *buf;
struct apm_ddr *ddr;
struct apm_ent *ent;
struct part *part;
ssize_t nbytes;
int error;
buf = calloc(nparts + 2, secsz);
@ -95,12 +99,7 @@ apm_write(int fd __unused, lba_t imgsz __unused, void *bootcode __unused)
strcpy(ent->ent_name, part->label);
}
error = mkimg_seek(fd, 0);
if (error == 0) {
nbytes = (nparts + 2) * secsz;
if (write(fd, buf, nbytes) != nbytes)
error = errno;
}
error = mkimg_write(fd, 0, buf, nparts + 2);
free(buf);
return (error);
}

10
bsd.c
View File

@ -38,6 +38,10 @@ __FBSDID("$FreeBSD$");
#include "mkimg.h"
#include "scheme.h"
#ifndef FS_NANDFS
#define FS_NANDFS 30
#endif
static struct mkimg_alias bsd_aliases[] = {
{ ALIAS_FREEBSD_NANDFS, ALIAS_INT2TYPE(FS_NANDFS) },
{ ALIAS_FREEBSD_SWAP, ALIAS_INT2TYPE(FS_SWAP) },
@ -107,11 +111,7 @@ bsd_write(int fd, lba_t imgsz, void *bootcode)
checksum ^= le16dec(p);
le16enc(&d->d_checksum, checksum);
error = mkimg_seek(fd, 0);
if (error == 0) {
if (write(fd, buf, BBSIZE) != BBSIZE)
error = errno;
}
error = mkimg_write(fd, 0, buf, BBSIZE / secsz);
free(buf);
return (error);
}

10
ebr.c
View File

@ -38,6 +38,10 @@ __FBSDID("$FreeBSD$");
#include "mkimg.h"
#include "scheme.h"
#ifndef DOSPTYP_FAT32
#define DOSPTYP_FAT32 0x0b
#endif
static struct mkimg_alias ebr_aliases[] = {
{ ALIAS_FAT32, ALIAS_INT2TYPE(DOSPTYP_FAT32) },
{ ALIAS_FREEBSD, ALIAS_INT2TYPE(DOSPTYP_386BSD) },
@ -100,11 +104,7 @@ ebr_write(int fd, lba_t imgsz __unused, void *bootcode __unused)
le32enc(&dp->dp_size, next->size + nsecs);
}
error = mkimg_seek(fd, block);
if (error == 0) {
if (write(fd, ebr, secsz) != (ssize_t)secsz)
error = errno;
}
error = mkimg_write(fd, block, ebr, 1);
if (error)
break;

47
gpt.c
View File

@ -42,6 +42,11 @@ __FBSDID("$FreeBSD$");
#include "mkimg.h"
#include "scheme.h"
#ifndef GPT_ENT_TYPE_FREEBSD_NANDFS
#define GPT_ENT_TYPE_FREEBSD_NANDFS \
{0x74ba7dd9,0xa689,0x11e1,0xbd,0x04,{0x00,0xe0,0x81,0x28,0x6a,0xcf}}
#endif
static uuid_t gpt_uuid_efi = GPT_ENT_TYPE_EFI;
static uuid_t gpt_uuid_freebsd = GPT_ENT_TYPE_FREEBSD;
static uuid_t gpt_uuid_freebsd_boot = GPT_ENT_TYPE_FREEBSD_BOOT;
@ -123,6 +128,21 @@ crc32(const void *buf, size_t sz)
return (crc ^ ~0U);
}
static void
gpt_uuid_enc(void *buf, const uuid_t *uuid)
{
uint8_t *p = buf;
int i;
le32enc(p, uuid->time_low);
le16enc(p + 4, uuid->time_mid);
le16enc(p + 6, uuid->time_hi_and_version);
p[8] = uuid->clock_seq_hi_and_reserved;
p[9] = uuid->clock_seq_low;
for (i = 0; i < _UUID_NODE_LEN; i++)
p[10 + i] = uuid->node[i];
}
static u_int
gpt_tblsz(void)
{
@ -145,19 +165,6 @@ gpt_metadata(u_int where)
return (secs);
}
static int
gpt_filewrite(int fd, lba_t blk, void *buf, ssize_t bufsz)
{
int error;
error = mkimg_seek(fd, blk);
if (error == 0) {
if (write(fd, buf, bufsz) != bufsz)
error = errno;
}
return (error);
}
static int
gpt_write_pmbr(int fd, lba_t blks, void *bootcode)
{
@ -183,7 +190,7 @@ gpt_write_pmbr(int fd, lba_t blks, void *bootcode)
le32enc(pmbr + DOSPARTOFF + 8, 1);
le32enc(pmbr + DOSPARTOFF + 12, secs);
le16enc(pmbr + DOSMAGICOFFSET, DOSMAGIC);
error = gpt_filewrite(fd, 0, pmbr, secsz);
error = mkimg_write(fd, 0, pmbr, 1);
free(pmbr);
return (error);
}
@ -202,9 +209,9 @@ gpt_mktbl(u_int tblsz)
STAILQ_FOREACH(part, &partlist, link) {
ent = tbl + part->index;
uuid_enc_le(&ent->ent_type, ALIAS_TYPE2PTR(part->type));
gpt_uuid_enc(&ent->ent_type, ALIAS_TYPE2PTR(part->type));
uuidgen(&uuid, 1);
uuid_enc_le(&ent->ent_uuid, &uuid);
gpt_uuid_enc(&ent->ent_uuid, &uuid);
le64enc(&ent->ent_lba_start, part->block);
le64enc(&ent->ent_lba_end, part->block + part->size - 1);
if (part->label != NULL) {
@ -230,7 +237,7 @@ gpt_write_hdr(int fd, struct gpt_hdr *hdr, uint64_t self, uint64_t alt,
hdr->hdr_crc_self = 0;
crc = crc32(hdr, offsetof(struct gpt_hdr, padding));
le64enc(&hdr->hdr_crc_self, crc);
return (gpt_filewrite(fd, self, hdr, secsz));
return (mkimg_write(fd, self, hdr, 1));
}
static int
@ -253,10 +260,10 @@ gpt_write(int fd, lba_t imgsz, void *bootcode)
tbl = gpt_mktbl(tblsz);
if (tbl == NULL)
return (errno);
error = gpt_filewrite(fd, 2, tbl, tblsz * secsz);
error = mkimg_write(fd, 2, tbl, tblsz);
if (error)
goto out;
error = gpt_filewrite(fd, imgsz - (tblsz + 1), tbl, tblsz * secsz);
error = mkimg_write(fd, imgsz - (tblsz + 1), tbl, tblsz);
if (error)
goto out;
@ -273,7 +280,7 @@ gpt_write(int fd, lba_t imgsz, void *bootcode)
le64enc(&hdr->hdr_lba_start, 2 + tblsz);
le64enc(&hdr->hdr_lba_end, imgsz - tblsz - 2);
uuidgen(&uuid, 1);
uuid_enc_le(&hdr->hdr_uuid, &uuid);
gpt_uuid_enc(&hdr->hdr_uuid, &uuid);
le32enc(&hdr->hdr_entries, nparts);
le32enc(&hdr->hdr_entsz, sizeof(struct gpt_ent));
crc = crc32(tbl, nparts * sizeof(struct gpt_ent));

10
mbr.c
View File

@ -38,6 +38,10 @@ __FBSDID("$FreeBSD$");
#include "mkimg.h"
#include "scheme.h"
#ifndef DOSPTYP_FAT32
#define DOSPTYP_FAT32 0x0b
#endif
static struct mkimg_alias mbr_aliases[] = {
{ ALIAS_EBR, ALIAS_INT2TYPE(DOSPTYP_EXT) },
{ ALIAS_FAT32, ALIAS_INT2TYPE(DOSPTYP_FAT32) },
@ -92,11 +96,7 @@ mbr_write(int fd, lba_t imgsz __unused, void *bootcode)
le32enc(&dp->dp_start, part->block);
le32enc(&dp->dp_size, part->size);
}
error = mkimg_seek(fd, 0);
if (error == 0) {
if (write(fd, mbr, secsz) != (ssize_t)secsz)
error = errno;
}
error = mkimg_write(fd, 0, mbr, 1);
free(mbr);
return (error);
}

15
mkimg.c
View File

@ -302,7 +302,7 @@ fdcopy(int src, int dst, uint64_t *count)
return (errno);
}
int
static int
mkimg_seek(int fd, lba_t blk)
{
off_t off;
@ -313,6 +313,19 @@ mkimg_seek(int fd, lba_t blk)
return (0);
}
int
mkimg_write(int fd, lba_t blk, void *buf, ssize_t len)
{
blk *= secsz;
if (lseek(fd, blk, SEEK_SET) != blk)
return (errno);
len *= secsz;
if (write(fd, buf, len) != len)
return (errno);
return (0);
}
static void
mkimg(int bfd)
{

View File

@ -67,6 +67,6 @@ round_block(lba_t n)
return ((n + b - 1) & ~(b - 1));
}
int mkimg_seek(int fd, lba_t blk);
int mkimg_write(int fd, lba_t blk, void *buf, ssize_t len);
#endif /* _MKIMG_MKIMG_H_ */

19
pc98.c
View File

@ -38,6 +38,19 @@ __FBSDID("$FreeBSD$");
#include "mkimg.h"
#include "scheme.h"
#ifndef PC98_MAGIC
#define PC98_MAGIC 0xaa55
#endif
#ifndef PC98_MAGICOFS
#define PC98_MAGICOFS 510
#endif
#ifndef PC98_NPARTS
#define PC98_NPARTS 16
#endif
#ifndef PC98_PTYP_386BSD
#define PC98_PTYP_386BSD 0xc494
#endif
#define PC98_BOOTCODESZ 8192
static struct mkimg_alias pc98_aliases[] = {
@ -93,11 +106,7 @@ pc98_write(int fd, lba_t imgsz __unused, void *bootcode)
if (part->label != NULL)
memcpy(dp->dp_name, part->label, strlen(part->label));
}
error = mkimg_seek(fd, 0);
if (error == 0) {
if (write(fd, buf, PC98_BOOTCODESZ) != PC98_BOOTCODESZ)
error = errno;
}
error = mkimg_write(fd, 0, buf, PC98_BOOTCODESZ / secsz);
free(buf);
return (error);
}

10
vtoc8.c
View File

@ -39,6 +39,10 @@ __FBSDID("$FreeBSD$");
#include "mkimg.h"
#include "scheme.h"
#ifndef VTOC_TAG_FREEBSD_NANDFS
#define VTOC_TAG_FREEBSD_NANDFS 0x0905
#endif
static struct mkimg_alias vtoc8_aliases[] = {
{ ALIAS_FREEBSD_NANDFS, ALIAS_INT2TYPE(VTOC_TAG_FREEBSD_NANDFS) },
{ ALIAS_FREEBSD_SWAP, ALIAS_INT2TYPE(VTOC_TAG_FREEBSD_SWAP) },
@ -99,11 +103,7 @@ vtoc8_write(int fd, lba_t imgsz, void *bootcode __unused)
sum ^= be16dec(p + ofs);
be16enc(&vtoc8.cksum, sum);
error = mkimg_seek(fd, 0);
if (error == 0) {
if (write(fd, &vtoc8, sizeof(vtoc8)) != sizeof(vtoc8))
error = errno;
}
error = mkimg_write(fd, 0, &vtoc8, 1);
return (error);
}