17f983a3marcel/*-
27f983a3marcel * Copyright (c) 2014 Juniper Networks, Inc.
37f983a3marcel * All rights reserved.
47f983a3marcel *
57f983a3marcel * Redistribution and use in source and binary forms, with or without
67f983a3marcel * modification, are permitted provided that the following conditions
77f983a3marcel * are met:
87f983a3marcel * 1. Redistributions of source code must retain the above copyright
97f983a3marcel *    notice, this list of conditions and the following disclaimer.
107f983a3marcel * 2. Redistributions in binary form must reproduce the above copyright
117f983a3marcel *    notice, this list of conditions and the following disclaimer in the
127f983a3marcel *    documentation and/or other materials provided with the distribution.
137f983a3marcel *
147f983a3marcel * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
157f983a3marcel * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
167f983a3marcel * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
177f983a3marcel * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
187f983a3marcel * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
197f983a3marcel * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
207f983a3marcel * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
217f983a3marcel * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
227f983a3marcel * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
237f983a3marcel * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
247f983a3marcel * SUCH DAMAGE.
257f983a3marcel */
267f983a3marcel
277f983a3marcel#include <sys/cdefs.h>
287f983a3marcel__FBSDID("$FreeBSD$");
297f983a3marcel
300dddaf6marcel#include <sys/errno.h>
315e84416marcel#include <stdint.h>
327f983a3marcel#include <stdlib.h>
330c6234cmarcel#include <string.h>
347f983a3marcel
35a31300amarcel#include <bsd.h>
365e84416marcel
37741bf12marcel#include "endian.h"
3824cdcc8marcel#include "image.h"
397f983a3marcel#include "mkimg.h"
407f983a3marcel#include "scheme.h"
417f983a3marcel
427f983a3marcelstatic struct mkimg_alias bsd_aliases[] = {
430c6234cmarcel    {	ALIAS_FREEBSD_NANDFS, ALIAS_INT2TYPE(FS_NANDFS) },
440c6234cmarcel    {	ALIAS_FREEBSD_SWAP, ALIAS_INT2TYPE(FS_SWAP) },
450c6234cmarcel    {	ALIAS_FREEBSD_UFS, ALIAS_INT2TYPE(FS_BSDFFS) },
460c6234cmarcel    {	ALIAS_FREEBSD_VINUM, ALIAS_INT2TYPE(FS_VINUM) },
470c6234cmarcel    {	ALIAS_FREEBSD_ZFS, ALIAS_INT2TYPE(FS_ZFS) },
48ace52aemarcel    {	ALIAS_NONE, 0 }
497f983a3marcel};
507f983a3marcel
519dc0698marcelstatic lba_t
529dc0698marcelbsd_metadata(u_int where, lba_t blk)
537f983a3marcel{
547f983a3marcel
559dc0698marcel	if (where == SCHEME_META_IMG_START)
563b59134marcel		blk += BSD_BOOTBLOCK_SIZE / secsz;
579dc0698marcel	else if (where == SCHEME_META_IMG_END)
589dc0698marcel		blk = round_cylinder(blk);
599dc0698marcel	else
609dc0698marcel		blk = round_block(blk);
619dc0698marcel	return (blk);
627f983a3marcel}
637f983a3marcel
640dddaf6marcelstatic int
6524cdcc8marcelbsd_write(lba_t imgsz, void *bootcode)
660dddaf6marcel{
670c6234cmarcel	u_char *buf, *p;
680c6234cmarcel	struct disklabel *d;
690c6234cmarcel	struct partition *dp;
700c6234cmarcel	struct part *part;
712cbd733marcel	int bsdparts, error, n;
720c6234cmarcel	uint16_t checksum;
730c6234cmarcel
743b59134marcel	buf = malloc(BSD_BOOTBLOCK_SIZE);
750c6234cmarcel	if (buf == NULL)
760c6234cmarcel		return (ENOMEM);
770c6234cmarcel	if (bootcode != NULL) {
783b59134marcel		memcpy(buf, bootcode, BSD_BOOTBLOCK_SIZE);
7960c8581marcel		memset(buf + secsz, 0, sizeof(struct disklabel));
800c6234cmarcel	} else
813b59134marcel		memset(buf, 0, BSD_BOOTBLOCK_SIZE);
820c6234cmarcel
832cbd733marcel	bsdparts = nparts + 1;	/* Account for c partition */
843b59134marcel	if (bsdparts < BSD_NPARTS_MIN)
853b59134marcel		bsdparts = BSD_NPARTS_MIN;
86c4d8b57marcel
8730dea1dmarcel	d = (void *)(buf + secsz);
883b59134marcel	le32enc(&d->d_magic, BSD_MAGIC);
890c6234cmarcel	le32enc(&d->d_secsize, secsz);
90c4d8b57marcel	le32enc(&d->d_nsectors, nsecs);
91c4d8b57marcel	le32enc(&d->d_ntracks, nheads);
92c4d8b57marcel	le32enc(&d->d_ncylinders, ncyls);
93c4d8b57marcel	le32enc(&d->d_secpercyl, nsecs * nheads);
940c6234cmarcel	le32enc(&d->d_secperunit, imgsz);
950c6234cmarcel	le16enc(&d->d_rpm, 3600);
963b59134marcel	le32enc(&d->d_magic2, BSD_MAGIC);
972cbd733marcel	le16enc(&d->d_npartitions, bsdparts);
983b59134marcel	le32enc(&d->d_bbsize, BSD_BOOTBLOCK_SIZE);
990c6234cmarcel
1003b59134marcel	dp = &d->d_partitions[BSD_PART_RAW];
10150a0b74marcel	le32enc(&dp->p_size, imgsz);
102f258aa5marcel	TAILQ_FOREACH(part, &partlist, link) {
1033b59134marcel		n = part->index + ((part->index >= BSD_PART_RAW) ? 1 : 0);
10450a0b74marcel		dp = &d->d_partitions[n];
1050c6234cmarcel		le32enc(&dp->p_size, part->size);
1060c6234cmarcel		le32enc(&dp->p_offset, part->block);
10760c8581marcel		le32enc(&dp->p_fsize, 0);
1080c6234cmarcel		dp->p_fstype = ALIAS_TYPE2INT(part->type);
10960c8581marcel		dp->p_frag = 0;
11060c8581marcel		le16enc(&dp->p_cpg, 0);
1110c6234cmarcel	}
1120c6234cmarcel
1132cbd733marcel	dp = &d->d_partitions[bsdparts];
1140c6234cmarcel	checksum = 0;
1152cbd733marcel	for (p = (void *)d; p < (u_char *)dp; p += 2)
1160c6234cmarcel		checksum ^= le16dec(p);
1170c6234cmarcel	le16enc(&d->d_checksum, checksum);
1180c6234cmarcel
1193b59134marcel	error = image_write(0, buf, BSD_BOOTBLOCK_SIZE / secsz);
1200c6234cmarcel	free(buf);
1210c6234cmarcel	return (error);
1220dddaf6marcel}
1230dddaf6marcel
1247f983a3marcelstatic struct mkimg_scheme bsd_scheme = {
1257f983a3marcel	.name = "bsd",
1263f2295fmarcel	.description = "BSD disk label",
1277f983a3marcel	.aliases = bsd_aliases,
1283f2295fmarcel	.metadata = bsd_metadata,
1290dddaf6marcel	.write = bsd_write,
1303b59134marcel	.nparts = BSD_NPARTS_MAX - 1,
1313b59134marcel	.bootcode = BSD_BOOTBLOCK_SIZE,
132cedbce4marcel	.maxsecsz = 512
1337f983a3marcel};
1347f983a3marcel
1357f983a3marcelSCHEME_DEFINE(bsd_scheme);
136