2013-08-04 02:37:05 +00:00
|
|
|
/*-
|
2014-03-20 00:38:17 +00:00
|
|
|
* Copyright (c) 2013,2014 Juniper Networks, Inc.
|
2013-08-04 02:37:05 +00:00
|
|
|
* 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 <sys/cdefs.h>
|
|
|
|
__FBSDID("$FreeBSD$");
|
|
|
|
|
|
|
|
#include <sys/types.h>
|
2014-03-20 00:38:17 +00:00
|
|
|
#include <sys/linker_set.h>
|
2014-03-19 21:44:51 +00:00
|
|
|
#include <sys/queue.h>
|
2013-08-04 02:37:05 +00:00
|
|
|
#include <err.h>
|
|
|
|
#include <errno.h>
|
|
|
|
#include <stdint.h>
|
|
|
|
#include <strings.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
|
2014-03-19 21:44:51 +00:00
|
|
|
#include "mkimg.h"
|
2013-08-04 02:37:05 +00:00
|
|
|
#include "scheme.h"
|
|
|
|
|
2014-03-20 00:38:17 +00:00
|
|
|
static struct mkimg_scheme *scheme;
|
2013-08-04 02:37:05 +00:00
|
|
|
static u_int secsz = 512;
|
|
|
|
|
|
|
|
int
|
|
|
|
scheme_select(const char *spec)
|
|
|
|
{
|
2014-03-20 00:38:17 +00:00
|
|
|
struct mkimg_scheme *s, **iter;
|
2013-08-04 02:37:05 +00:00
|
|
|
|
2014-03-20 00:38:17 +00:00
|
|
|
SET_FOREACH(iter, schemes) {
|
|
|
|
s = *iter;
|
|
|
|
if (strcasecmp(spec, s->name) == 0) {
|
|
|
|
scheme = s;
|
2013-08-04 02:37:05 +00:00
|
|
|
return (0);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return (EINVAL);
|
|
|
|
}
|
|
|
|
|
2014-03-20 00:38:17 +00:00
|
|
|
struct mkimg_scheme *
|
2013-08-04 02:37:05 +00:00
|
|
|
scheme_selected(void)
|
|
|
|
{
|
|
|
|
|
|
|
|
return (scheme);
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
2014-03-20 00:38:17 +00:00
|
|
|
scheme_check_part(struct part *p)
|
2013-08-04 02:37:05 +00:00
|
|
|
{
|
2014-03-20 02:27:25 +00:00
|
|
|
struct mkimg_alias *alias, *iter;
|
2013-08-04 02:37:05 +00:00
|
|
|
|
2014-03-20 00:38:17 +00:00
|
|
|
warnx("part(%s): index=%u, type=`%s', offset=%ju, size=%ju",
|
2014-03-20 02:27:25 +00:00
|
|
|
scheme->name, p->index, p->alias, (uintmax_t)p->offset,
|
2014-03-20 00:38:17 +00:00
|
|
|
(uintmax_t)p->size);
|
2014-03-19 21:44:51 +00:00
|
|
|
|
2014-03-20 02:27:25 +00:00
|
|
|
/* Check the partition type alias */
|
|
|
|
alias = NULL;
|
|
|
|
iter = scheme->aliases;
|
|
|
|
while (iter->name != NULL) {
|
|
|
|
if (strcasecmp(p->alias, iter->name) == 0) {
|
|
|
|
alias = iter;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
iter++;
|
|
|
|
}
|
|
|
|
if (alias == NULL)
|
|
|
|
return (EINVAL);
|
2014-03-21 03:27:42 +00:00
|
|
|
p->type = iter->type;
|
2013-08-04 02:37:05 +00:00
|
|
|
return (0);
|
|
|
|
}
|
|
|
|
|
|
|
|
u_int
|
|
|
|
scheme_max_parts(void)
|
|
|
|
{
|
2014-03-20 00:38:17 +00:00
|
|
|
|
|
|
|
return (scheme->nparts);
|
2013-08-04 02:37:05 +00:00
|
|
|
}
|
|
|
|
|
2014-03-20 20:14:26 +00:00
|
|
|
uint64_t
|
|
|
|
scheme_round(uint64_t sz)
|
|
|
|
{
|
|
|
|
|
|
|
|
sz = (sz + secsz - 1) & ~(secsz - 1);
|
|
|
|
return (sz);
|
|
|
|
}
|
|
|
|
|
2013-08-04 02:37:05 +00:00
|
|
|
off_t
|
|
|
|
scheme_first_offset(u_int parts)
|
|
|
|
{
|
2014-03-20 19:37:30 +00:00
|
|
|
u_int secs;
|
2013-08-04 02:37:05 +00:00
|
|
|
|
2014-03-20 19:37:30 +00:00
|
|
|
secs = scheme->metadata(SCHEME_META_IMG_START, parts, secsz) +
|
|
|
|
scheme->metadata(SCHEME_META_PART_BEFORE, 0, secsz);
|
|
|
|
return (secs * secsz);
|
2013-08-04 02:37:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
off_t
|
|
|
|
scheme_next_offset(off_t off, uint64_t sz)
|
|
|
|
{
|
2014-03-20 19:37:30 +00:00
|
|
|
u_int secs;
|
2013-08-04 02:37:05 +00:00
|
|
|
|
2014-03-20 19:37:30 +00:00
|
|
|
secs = scheme->metadata(SCHEME_META_PART_AFTER, 0, secsz) +
|
|
|
|
scheme->metadata(SCHEME_META_PART_BEFORE, 0, secsz);
|
|
|
|
sz += (secs * secsz);
|
2013-08-04 02:37:05 +00:00
|
|
|
return (off + sz);
|
|
|
|
}
|
|
|
|
|
2014-03-20 20:14:26 +00:00
|
|
|
int
|
2013-08-04 02:37:05 +00:00
|
|
|
scheme_write(int fd, off_t off)
|
|
|
|
{
|
2014-03-20 19:37:30 +00:00
|
|
|
u_int secs;
|
2014-03-20 20:14:26 +00:00
|
|
|
int error;
|
2014-03-20 00:38:17 +00:00
|
|
|
|
2014-03-20 19:37:30 +00:00
|
|
|
/* Fixup offset: it has an extra metadata before the partition */
|
|
|
|
secs = scheme->metadata(SCHEME_META_PART_BEFORE, 0, secsz);
|
|
|
|
off -= (secs * secsz);
|
|
|
|
|
|
|
|
secs = scheme->metadata(SCHEME_META_IMG_END, nparts, secsz);
|
|
|
|
off += (secs * secsz);
|
2014-03-20 20:14:26 +00:00
|
|
|
if (ftruncate(fd, off) == -1)
|
|
|
|
return (errno);
|
|
|
|
|
|
|
|
error = scheme->write(fd, off, nparts, secsz);
|
|
|
|
return (error);
|
2013-08-04 02:37:05 +00:00
|
|
|
}
|