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
42292341Semaste#ifndef DOSPTYP_FAT16B
43292341Semaste#define	DOSPTYP_FAT16B	0x06
44292341Semaste#endif
45268161Smarcel#ifndef DOSPTYP_FAT32
46268161Smarcel#define	DOSPTYP_FAT32	0x0b
47268161Smarcel#endif
48278968Semaste#ifndef DOSPTYP_EFI
49278968Semaste#define	DOSPTYP_EFI	0xef
50278968Semaste#endif
51268161Smarcel
52263409Smarcelstatic struct mkimg_alias mbr_aliases[] = {
53263673Smarcel    {	ALIAS_EBR, ALIAS_INT2TYPE(DOSPTYP_EXT) },
54278968Semaste    {	ALIAS_EFI, ALIAS_INT2TYPE(DOSPTYP_EFI) },
55292341Semaste    {	ALIAS_FAT16B, ALIAS_INT2TYPE(DOSPTYP_FAT16B) },
56263673Smarcel    {	ALIAS_FAT32, ALIAS_INT2TYPE(DOSPTYP_FAT32) },
57263652Smarcel    {	ALIAS_FREEBSD, ALIAS_INT2TYPE(DOSPTYP_386BSD) },
58287122Smarcel    {	ALIAS_NTFS, ALIAS_INT2TYPE(DOSPTYP_NTFS) },
59263652Smarcel    {	ALIAS_NONE, 0 }		/* Keep last! */
60263409Smarcel};
61263409Smarcel
62272030Smarcelstatic lba_t
63272030Smarcelmbr_metadata(u_int where, lba_t blk)
64263409Smarcel{
65263409Smarcel
66272030Smarcel	blk += (where == SCHEME_META_IMG_START) ? 1 : 0;
67272030Smarcel	return (round_track(blk));
68263409Smarcel}
69263409Smarcel
70263706Smarcelstatic void
71272776Smarcelmbr_chs(u_char *cylp, u_char *hdp, u_char *secp, lba_t lba)
72263706Smarcel{
73272776Smarcel	u_int cyl, hd, sec;
74263706Smarcel
75272776Smarcel	mkimg_chs(lba, 1023, &cyl, &hd, &sec);
76272776Smarcel	*cylp = cyl;
77272776Smarcel	*hdp = hd;
78272776Smarcel	*secp = (sec & 0x3f) | ((cyl >> 2) & 0xc0);
79263706Smarcel}
80263706Smarcel
81263442Smarcelstatic int
82268161Smarcelmbr_write(lba_t imgsz __unused, void *bootcode)
83263442Smarcel{
84263652Smarcel	u_char *mbr;
85263652Smarcel	struct dos_partition *dpbase, *dp;
86263652Smarcel	struct part *part;
87272776Smarcel	lba_t size;
88263653Smarcel	int error;
89263652Smarcel
90263652Smarcel	mbr = malloc(secsz);
91263652Smarcel	if (mbr == NULL)
92263652Smarcel		return (ENOMEM);
93263652Smarcel	if (bootcode != NULL) {
94263652Smarcel		memcpy(mbr, bootcode, DOSPARTOFF);
95263652Smarcel		memset(mbr + DOSPARTOFF, 0, secsz - DOSPARTOFF);
96263652Smarcel	} else
97263652Smarcel		memset(mbr, 0, secsz);
98263654Smarcel	le16enc(mbr + DOSMAGICOFFSET, DOSMAGIC);
99263652Smarcel	dpbase = (void *)(mbr + DOSPARTOFF);
100263652Smarcel	STAILQ_FOREACH(part, &partlist, link) {
101272776Smarcel		size = round_track(part->size);
102263652Smarcel		dp = dpbase + part->index;
103263652Smarcel		dp->dp_flag = (part->index == 0 && bootcode != NULL) ? 0x80 : 0;
104263706Smarcel		mbr_chs(&dp->dp_scyl, &dp->dp_shd, &dp->dp_ssect,
105263706Smarcel		    part->block);
106263652Smarcel		dp->dp_typ = ALIAS_TYPE2INT(part->type);
107263706Smarcel		mbr_chs(&dp->dp_ecyl, &dp->dp_ehd, &dp->dp_esect,
108272776Smarcel		    part->block + size - 1);
109263841Smarcel		le32enc(&dp->dp_start, part->block);
110272776Smarcel		le32enc(&dp->dp_size, size);
111263652Smarcel	}
112268161Smarcel	error = image_write(0, mbr, 1);
113263652Smarcel	free(mbr);
114263653Smarcel	return (error);
115263442Smarcel}
116263442Smarcel
117263409Smarcelstatic struct mkimg_scheme mbr_scheme = {
118263409Smarcel	.name = "mbr",
119263409Smarcel	.description = "Master Boot Record",
120263409Smarcel	.aliases = mbr_aliases,
121263440Smarcel	.metadata = mbr_metadata,
122263442Smarcel	.write = mbr_write,
123263652Smarcel	.bootcode = 512,
124263700Smarcel	.nparts = NDOSPART,
125263700Smarcel	.maxsecsz = 4096
126263409Smarcel};
127263409Smarcel
128263409SmarcelSCHEME_DEFINE(mbr_scheme);
129