Switch to the image API:

1.  Replace calls to mkimg_set_size() with calls to image_set_size()
2.  Remove the mkimg_set_size() function
3.  As above but for mkimg_write() and image_write()

Note that this breaks mkimg(1). The image API has no implementation.
Hence doing it on my branch :-)
This commit is contained in:
marcel 2014-05-07 17:33:31 +00:00
parent 070a8547ae
commit 24cdcc8e3f
13 changed files with 42 additions and 59 deletions

5
apm.c
View File

@ -35,6 +35,7 @@ __FBSDID("$FreeBSD$");
#include <string.h>
#include <unistd.h>
#include "image.h"
#include "mkimg.h"
#include "scheme.h"
@ -63,7 +64,7 @@ apm_metadata(u_int where)
}
static int
apm_write(int fd, lba_t imgsz, void *bootcode __unused)
apm_write(lba_t imgsz, void *bootcode __unused)
{
u_char *buf;
struct apm_ddr *ddr;
@ -99,7 +100,7 @@ apm_write(int fd, lba_t imgsz, void *bootcode __unused)
strcpy(ent->ent_name, part->label);
}
error = mkimg_write(fd, 0, buf, nparts + 2);
error = image_write(0, buf, nparts + 2);
free(buf);
return (error);
}

7
bsd.c
View File

@ -35,6 +35,7 @@ __FBSDID("$FreeBSD$");
#include <string.h>
#include <unistd.h>
#include "image.h"
#include "mkimg.h"
#include "scheme.h"
@ -61,7 +62,7 @@ bsd_metadata(u_int where)
}
static int
bsd_write(int fd, lba_t imgsz, void *bootcode)
bsd_write(lba_t imgsz, void *bootcode)
{
u_char *buf, *p;
struct disklabel *d;
@ -80,7 +81,7 @@ bsd_write(int fd, lba_t imgsz, void *bootcode)
memset(buf, 0, BBSIZE);
imgsz = ncyls * nheads * nsecs;
error = mkimg_set_size(fd, imgsz);
error = image_set_size(imgsz);
if (error)
return (error);
@ -113,7 +114,7 @@ bsd_write(int fd, lba_t imgsz, void *bootcode)
checksum ^= le16dec(p);
le16enc(&d->d_checksum, checksum);
error = mkimg_write(fd, 0, buf, BBSIZE / secsz);
error = image_write(0, buf, BBSIZE / secsz);
free(buf);
return (error);
}

5
ebr.c
View File

@ -35,6 +35,7 @@ __FBSDID("$FreeBSD$");
#include <string.h>
#include <unistd.h>
#include "image.h"
#include "mkimg.h"
#include "scheme.h"
@ -67,7 +68,7 @@ ebr_chs(u_char *cyl, u_char *hd, u_char *sec, uint32_t lba __unused)
}
static int
ebr_write(int fd, lba_t imgsz __unused, void *bootcode __unused)
ebr_write(lba_t imgsz __unused, void *bootcode __unused)
{
u_char *ebr;
struct dos_partition *dp;
@ -104,7 +105,7 @@ ebr_write(int fd, lba_t imgsz __unused, void *bootcode __unused)
le32enc(&dp->dp_size, next->size + nsecs);
}
error = mkimg_write(fd, block, ebr, 1);
error = image_write(block, ebr, 1);
if (error)
break;

22
gpt.c
View File

@ -39,6 +39,7 @@ __FBSDID("$FreeBSD$");
#include <unistd.h>
#include <uuid.h>
#include "image.h"
#include "mkimg.h"
#include "scheme.h"
@ -166,7 +167,7 @@ gpt_metadata(u_int where)
}
static int
gpt_write_pmbr(int fd, lba_t blks, void *bootcode)
gpt_write_pmbr(lba_t blks, void *bootcode)
{
u_char *pmbr;
uint32_t secs;
@ -190,7 +191,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 = mkimg_write(fd, 0, pmbr, 1);
error = image_write(0, pmbr, 1);
free(pmbr);
return (error);
}
@ -226,8 +227,7 @@ gpt_mktbl(u_int tblsz)
}
static int
gpt_write_hdr(int fd, struct gpt_hdr *hdr, uint64_t self, uint64_t alt,
uint64_t tbl)
gpt_write_hdr(struct gpt_hdr *hdr, uint64_t self, uint64_t alt, uint64_t tbl)
{
uint32_t crc;
@ -237,11 +237,11 @@ 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 (mkimg_write(fd, self, hdr, 1));
return (image_write(self, hdr, 1));
}
static int
gpt_write(int fd, lba_t imgsz, void *bootcode)
gpt_write(lba_t imgsz, void *bootcode)
{
uuid_t uuid;
struct gpt_ent *tbl;
@ -251,7 +251,7 @@ gpt_write(int fd, lba_t imgsz, void *bootcode)
int error;
/* PMBR */
error = gpt_write_pmbr(fd, imgsz, bootcode);
error = gpt_write_pmbr(imgsz, bootcode);
if (error)
return (error);
@ -260,10 +260,10 @@ gpt_write(int fd, lba_t imgsz, void *bootcode)
tbl = gpt_mktbl(tblsz);
if (tbl == NULL)
return (errno);
error = mkimg_write(fd, 2, tbl, tblsz);
error = image_write(2, tbl, tblsz);
if (error)
goto out;
error = mkimg_write(fd, imgsz - (tblsz + 1), tbl, tblsz);
error = image_write(imgsz - (tblsz + 1), tbl, tblsz);
if (error)
goto out;
@ -285,9 +285,9 @@ gpt_write(int fd, lba_t imgsz, void *bootcode)
le32enc(&hdr->hdr_entsz, sizeof(struct gpt_ent));
crc = crc32(tbl, nparts * sizeof(struct gpt_ent));
le32enc(&hdr->hdr_crc_table, crc);
error = gpt_write_hdr(fd, hdr, 1, imgsz - 1, 2);
error = gpt_write_hdr(hdr, 1, imgsz - 1, 2);
if (!error)
error = gpt_write_hdr(fd, hdr, imgsz - 1, 1, imgsz - tblsz - 1);
error = gpt_write_hdr(hdr, imgsz - 1, 1, imgsz - tblsz - 1);
free(hdr);
out:

View File

@ -33,8 +33,8 @@ __FBSDID("$FreeBSD$");
#include <stdlib.h>
#include <unistd.h>
#include "mkimg.h"
#include "image.h"
#include "mkimg.h"
#define BUFFER_SIZE (1024*1024)

View File

@ -29,6 +29,8 @@
#ifndef _MKIMG_IMAGE_H_
#define _MKIMG_IMAGE_H_
typedef int64_t lba_t;
int image_copyin(lba_t blk, int fd, uint64_t *sizep);
int image_set_size(lba_t blk);
int image_write(lba_t blk, void *buf, ssize_t len);

5
mbr.c
View File

@ -35,6 +35,7 @@ __FBSDID("$FreeBSD$");
#include <string.h>
#include <unistd.h>
#include "image.h"
#include "mkimg.h"
#include "scheme.h"
@ -68,7 +69,7 @@ mbr_chs(u_char *cyl, u_char *hd, u_char *sec, uint32_t lba __unused)
}
static int
mbr_write(int fd, lba_t imgsz __unused, void *bootcode)
mbr_write(lba_t imgsz __unused, void *bootcode)
{
u_char *mbr;
struct dos_partition *dpbase, *dp;
@ -96,7 +97,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_write(fd, 0, mbr, 1);
error = image_write(0, mbr, 1);
free(mbr);
return (error);
}

25
mkimg.c
View File

@ -42,6 +42,7 @@ __FBSDID("$FreeBSD$");
#include <sysexits.h>
#include <unistd.h>
#include "image.h"
#include "mkimg.h"
#include "scheme.h"
@ -316,28 +317,6 @@ fdcopy(int src, lba_t sblk, int dst, lba_t dblk, uint64_t *count)
return (errno);
}
int
mkimg_set_size(int fd, lba_t blk)
{
if (ftruncate(fd, blk * secsz) == -1)
return (errno);
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 ofd, int bfd)
{
@ -401,7 +380,7 @@ mkimg(int ofd, int bfd)
}
block = scheme_metadata(SCHEME_META_IMG_END, block);
error = (scheme_write(ofd, block));
error = (scheme_write(block));
}
int

View File

@ -31,8 +31,6 @@
#include <sys/queue.h>
typedef int64_t lba_t;
struct part {
STAILQ_ENTRY(part) link;
char *alias; /* Partition type alias. */
@ -67,7 +65,4 @@ round_block(lba_t n)
return ((n + b - 1) & ~(b - 1));
}
int mkimg_set_size(int fd, lba_t blk);
int mkimg_write(int fd, lba_t blk, void *buf, ssize_t len);
#endif /* _MKIMG_MKIMG_H_ */

5
pc98.c
View File

@ -35,6 +35,7 @@ __FBSDID("$FreeBSD$");
#include <string.h>
#include <unistd.h>
#include "image.h"
#include "mkimg.h"
#include "scheme.h"
@ -77,7 +78,7 @@ pc98_chs(u_short *cyl, u_char *hd, u_char *sec, uint32_t lba __unused)
}
static int
pc98_write(int fd, lba_t imgsz __unused, void *bootcode)
pc98_write(lba_t imgsz __unused, void *bootcode)
{
struct part *part;
struct pc98_partition *dpbase, *dp;
@ -106,7 +107,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_write(fd, 0, buf, PC98_BOOTCODESZ / secsz);
error = image_write(0, buf, PC98_BOOTCODESZ / secsz);
free(buf);
return (error);
}

View File

@ -38,6 +38,7 @@ __FBSDID("$FreeBSD$");
#include <string.h>
#include <unistd.h>
#include "image.h"
#include "mkimg.h"
#include "scheme.h"
@ -181,7 +182,7 @@ scheme_metadata(u_int where, lba_t start)
}
int
scheme_write(int fd, lba_t end)
scheme_write(lba_t end)
{
u_int cylsz;
int error;
@ -189,8 +190,8 @@ scheme_write(int fd, lba_t end)
cylsz = nsecs * nheads;
ncyls = (end + cylsz - 1) / cylsz;
error = mkimg_set_size(fd, end);
error = image_set_size(end);
if (!error)
error = scheme->write(fd, end, bootcode);
error = scheme->write(end, bootcode);
return (error);
}

View File

@ -67,7 +67,7 @@ struct mkimg_scheme {
#define SCHEME_META_IMG_END 2
#define SCHEME_META_PART_BEFORE 3
#define SCHEME_META_PART_AFTER 4
int (*write)(int, lba_t, void *);
int (*write)(lba_t, void *);
u_int nparts;
u_int labellen;
u_int bootcode;
@ -85,6 +85,6 @@ 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(int, lba_t);
int scheme_write(lba_t);
#endif /* _MKIMG_SCHEME_H_ */

View File

@ -36,6 +36,7 @@ __FBSDID("$FreeBSD$");
#include <string.h>
#include <unistd.h>
#include "image.h"
#include "mkimg.h"
#include "scheme.h"
@ -62,7 +63,7 @@ vtoc8_metadata(u_int where)
}
static int
vtoc8_write(int fd, lba_t imgsz, void *bootcode __unused)
vtoc8_write(lba_t imgsz, void *bootcode __unused)
{
struct vtoc8 vtoc8;
struct part *part;
@ -86,7 +87,7 @@ vtoc8_write(int fd, lba_t imgsz, void *bootcode __unused)
be16enc(&vtoc8.nsecs, nsecs);
be16enc(&vtoc8.magic, VTOC_MAGIC);
error = mkimg_set_size(fd, imgsz);
error = image_set_size(imgsz);
if (error)
return (error);
@ -105,7 +106,7 @@ vtoc8_write(int fd, lba_t imgsz, void *bootcode __unused)
sum ^= be16dec(p + ofs);
be16enc(&vtoc8.cksum, sum);
error = mkimg_write(fd, 0, &vtoc8, 1);
error = image_write(0, &vtoc8, 1);
return (error);
}