mbr.c revision 263652
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: user/marcel/mkimg/mbr.c 263652 2014-03-22 23:34:35Z marcel $");
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
38263409Smarcel#include "mkimg.h"
39263409Smarcel#include "scheme.h"
40263409Smarcel
41263409Smarcelstatic struct mkimg_alias mbr_aliases[] = {
42263652Smarcel    {	ALIAS_FREEBSD, ALIAS_INT2TYPE(DOSPTYP_386BSD) },
43263652Smarcel    {	ALIAS_NONE, 0 }		/* Keep last! */
44263409Smarcel};
45263409Smarcel
46263440Smarcelstatic u_int
47263440Smarcelmbr_metadata(u_int where, u_int parts __unused, u_int secsz __unused)
48263409Smarcel{
49263440Smarcel	u_int secs;
50263409Smarcel
51263440Smarcel	secs = (where == SCHEME_META_IMG_START) ? 1 : 0;
52263440Smarcel	return (secs);
53263409Smarcel}
54263409Smarcel
55263442Smarcelstatic int
56263652Smarcelmbr_write(int fd, off_t imgsz __unused, u_int parts __unused, u_int secsz,
57263652Smarcel    void *bootcode)
58263442Smarcel{
59263652Smarcel	u_char *mbr;
60263652Smarcel	struct dos_partition *dpbase, *dp;
61263652Smarcel	struct part *part;
62263652Smarcel
63263652Smarcel	mbr = malloc(secsz);
64263652Smarcel	if (mbr == NULL)
65263652Smarcel		return (ENOMEM);
66263652Smarcel	if (bootcode != NULL) {
67263652Smarcel		memcpy(mbr, bootcode, DOSPARTOFF);
68263652Smarcel		memset(mbr + DOSPARTOFF, 0, secsz - DOSPARTOFF);
69263652Smarcel	} else
70263652Smarcel		memset(mbr, 0, secsz);
71263652Smarcel	dpbase = (void *)(mbr + DOSPARTOFF);
72263652Smarcel	STAILQ_FOREACH(part, &partlist, link) {
73263652Smarcel		dp = dpbase + part->index;
74263652Smarcel		dp->dp_flag = (part->index == 0 && bootcode != NULL) ? 0x80 : 0;
75263652Smarcel		dp->dp_shd = dp->dp_ssect = dp->dp_scyl = 0xff;	/* XXX */
76263652Smarcel		dp->dp_typ = ALIAS_TYPE2INT(part->type);
77263652Smarcel		dp->dp_ehd = dp->dp_esect = dp->dp_ecyl = 0xff;	/* XXX */
78263652Smarcel		le32enc(&dp[part->index].dp_start, part->offset / secsz);
79263652Smarcel		le32enc(&dp[part->index].dp_size, part->size / secsz);
80263652Smarcel	}
81263652Smarcel	if (lseek(fd, 0, SEEK_SET) != 0 || write(fd, mbr, secsz) != secsz) {
82263652Smarcel		free(mbr);
83263652Smarcel		return (errno);
84263652Smarcel	}
85263652Smarcel	free(mbr);
86263652Smarcel	return (0);
87263442Smarcel}
88263442Smarcel
89263409Smarcelstatic struct mkimg_scheme mbr_scheme = {
90263409Smarcel	.name = "mbr",
91263409Smarcel	.description = "Master Boot Record",
92263409Smarcel	.aliases = mbr_aliases,
93263440Smarcel	.metadata = mbr_metadata,
94263442Smarcel	.write = mbr_write,
95263652Smarcel	.bootcode = 512,
96263440Smarcel	.nparts = NDOSPART
97263409Smarcel};
98263409Smarcel
99263409SmarcelSCHEME_DEFINE(mbr_scheme);
100