1263409Smarcel/*-
2263409Smarcel * Copyright (c) 2014 Juniper Networks, Inc.
3263409Smarcel * All rights reserved.
4263409Smarcel *
5263409Smarcel * Redistribution and use in source and binary forms, with or without
6263409Smarcel * modification, are permitted provided that the following conditions
7263409Smarcel * are met:
8263409Smarcel * 1. Redistributions of source code must retain the above copyright
9263409Smarcel *    notice, this list of conditions and the following disclaimer.
10263409Smarcel * 2. Redistributions in binary form must reproduce the above copyright
11263409Smarcel *    notice, this list of conditions and the following disclaimer in the
12263409Smarcel *    documentation and/or other materials provided with the distribution.
13263409Smarcel *
14263409Smarcel * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15263409Smarcel * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16263409Smarcel * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17263409Smarcel * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18263409Smarcel * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19263409Smarcel * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20263409Smarcel * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21263409Smarcel * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22263409Smarcel * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23263409Smarcel * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24263409Smarcel * SUCH DAMAGE.
25263409Smarcel */
26263409Smarcel
27263409Smarcel#include <sys/cdefs.h>
28263409Smarcel__FBSDID("$FreeBSD$");
29263409Smarcel
30263409Smarcel#include <sys/types.h>
31263409Smarcel#include <sys/diskmbr.h>
32263652Smarcel#include <sys/endian.h>
33263442Smarcel#include <sys/errno.h>
34263409Smarcel#include <stdlib.h>
35263652Smarcel#include <string.h>
36263652Smarcel#include <unistd.h>
37263409Smarcel
38268161Smarcel#include "image.h"
39263409Smarcel#include "mkimg.h"
40263409Smarcel#include "scheme.h"
41263409Smarcel
42268161Smarcel#ifndef DOSPTYP_FAT32
43268161Smarcel#define	DOSPTYP_FAT32	0x0b
44268161Smarcel#endif
45268161Smarcel
46263409Smarcelstatic struct mkimg_alias mbr_aliases[] = {
47263673Smarcel    {	ALIAS_EBR, ALIAS_INT2TYPE(DOSPTYP_EXT) },
48263673Smarcel    {	ALIAS_FAT32, ALIAS_INT2TYPE(DOSPTYP_FAT32) },
49263652Smarcel    {	ALIAS_FREEBSD, ALIAS_INT2TYPE(DOSPTYP_386BSD) },
50263652Smarcel    {	ALIAS_NONE, 0 }		/* Keep last! */
51263409Smarcel};
52263409Smarcel
53272030Smarcelstatic lba_t
54272030Smarcelmbr_metadata(u_int where, lba_t blk)
55263409Smarcel{
56263409Smarcel
57272030Smarcel	blk += (where == SCHEME_META_IMG_START) ? 1 : 0;
58272030Smarcel	return (round_track(blk));
59263409Smarcel}
60263409Smarcel
61263706Smarcelstatic void
62263706Smarcelmbr_chs(u_char *cyl, u_char *hd, u_char *sec, uint32_t lba __unused)
63263706Smarcel{
64263706Smarcel
65263706Smarcel	*cyl = 0xff;		/* XXX */
66263706Smarcel	*hd = 0xff;		/* XXX */
67263706Smarcel	*sec = 0xff;		/* XXX */
68263706Smarcel}
69263706Smarcel
70263442Smarcelstatic int
71268161Smarcelmbr_write(lba_t imgsz __unused, void *bootcode)
72263442Smarcel{
73263652Smarcel	u_char *mbr;
74263652Smarcel	struct dos_partition *dpbase, *dp;
75263652Smarcel	struct part *part;
76263653Smarcel	int error;
77263652Smarcel
78263652Smarcel	mbr = malloc(secsz);
79263652Smarcel	if (mbr == NULL)
80263652Smarcel		return (ENOMEM);
81263652Smarcel	if (bootcode != NULL) {
82263652Smarcel		memcpy(mbr, bootcode, DOSPARTOFF);
83263652Smarcel		memset(mbr + DOSPARTOFF, 0, secsz - DOSPARTOFF);
84263652Smarcel	} else
85263652Smarcel		memset(mbr, 0, secsz);
86263654Smarcel	le16enc(mbr + DOSMAGICOFFSET, DOSMAGIC);
87263652Smarcel	dpbase = (void *)(mbr + DOSPARTOFF);
88263652Smarcel	STAILQ_FOREACH(part, &partlist, link) {
89263652Smarcel		dp = dpbase + part->index;
90263652Smarcel		dp->dp_flag = (part->index == 0 && bootcode != NULL) ? 0x80 : 0;
91263706Smarcel		mbr_chs(&dp->dp_scyl, &dp->dp_shd, &dp->dp_ssect,
92263706Smarcel		    part->block);
93263652Smarcel		dp->dp_typ = ALIAS_TYPE2INT(part->type);
94263706Smarcel		mbr_chs(&dp->dp_ecyl, &dp->dp_ehd, &dp->dp_esect,
95263706Smarcel		    part->block + part->size - 1);
96263841Smarcel		le32enc(&dp->dp_start, part->block);
97263841Smarcel		le32enc(&dp->dp_size, part->size);
98263652Smarcel	}
99268161Smarcel	error = image_write(0, mbr, 1);
100263652Smarcel	free(mbr);
101263653Smarcel	return (error);
102263442Smarcel}
103263442Smarcel
104263409Smarcelstatic struct mkimg_scheme mbr_scheme = {
105263409Smarcel	.name = "mbr",
106263409Smarcel	.description = "Master Boot Record",
107263409Smarcel	.aliases = mbr_aliases,
108263440Smarcel	.metadata = mbr_metadata,
109263442Smarcel	.write = mbr_write,
110263652Smarcel	.bootcode = 512,
111263700Smarcel	.nparts = NDOSPART,
112263700Smarcel	.maxsecsz = 4096
113263409Smarcel};
114263409Smarcel
115263409SmarcelSCHEME_DEFINE(mbr_scheme);
116