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);
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
|
|
|
off_t
|
|
|
|
scheme_first_offset(u_int parts)
|
|
|
|
{
|
|
|
|
off_t off;
|
|
|
|
|
2014-03-20 00:38:17 +00:00
|
|
|
off = scheme->get_leader(parts);
|
2013-08-04 02:37:05 +00:00
|
|
|
off *= secsz;
|
|
|
|
return (off);
|
|
|
|
}
|
|
|
|
|
|
|
|
off_t
|
|
|
|
scheme_next_offset(off_t off, uint64_t sz)
|
|
|
|
{
|
|
|
|
|
|
|
|
sz = (sz + secsz - 1) & ~(secsz - 1);
|
2014-03-20 00:38:17 +00:00
|
|
|
sz += scheme->padding * secsz;
|
2013-08-04 02:37:05 +00:00
|
|
|
return (off + sz);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
scheme_write(int fd, off_t off)
|
|
|
|
{
|
2014-03-20 00:38:17 +00:00
|
|
|
off_t trailer;
|
|
|
|
|
|
|
|
trailer = scheme->get_trailer(nparts) * secsz;
|
|
|
|
ftruncate(fd, off + trailer);
|
2013-08-04 02:37:05 +00:00
|
|
|
}
|