xref: /freebsd/usr.bin/mkimg/mbr.c (revision 74d9553e43cfafc29448d0bb836916aa21dea0de)
1 /*-
2  * Copyright (c) 2014 Juniper Networks, Inc.
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  *
14  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24  * SUCH DAMAGE.
25  */
26 
27 #include <sys/cdefs.h>
28 __FBSDID("$FreeBSD$");
29 
30 #include <sys/errno.h>
31 #include <stdint.h>
32 #include <stdlib.h>
33 #include <string.h>
34 #include <unistd.h>
35 
36 #include <mbr.h>
37 
38 #include "endian.h"
39 #include "image.h"
40 #include "mkimg.h"
41 #include "scheme.h"
42 
43 static struct mkimg_alias mbr_aliases[] = {
44     {	ALIAS_EBR, ALIAS_INT2TYPE(DOSPTYP_EXT) },
45     {	ALIAS_EFI, ALIAS_INT2TYPE(DOSPTYP_EFI) },
46     {	ALIAS_FAT16B, ALIAS_INT2TYPE(DOSPTYP_FAT16) },
47     {	ALIAS_FAT32, ALIAS_INT2TYPE(DOSPTYP_FAT32) },
48     {	ALIAS_FREEBSD, ALIAS_INT2TYPE(DOSPTYP_386BSD) },
49     {	ALIAS_NTFS, ALIAS_INT2TYPE(DOSPTYP_NTFS) },
50     {	ALIAS_PPCBOOT, ALIAS_INT2TYPE(DOSPTYP_PPCBOOT) },
51     {	ALIAS_NONE, 0 }		/* Keep last! */
52 };
53 
54 static lba_t
55 mbr_metadata(u_int where, lba_t blk)
56 {
57 
58 	blk += (where == SCHEME_META_IMG_START) ? 1 : 0;
59 	return (round_track(blk));
60 }
61 
62 static void
63 mbr_chs(u_char *cylp, u_char *hdp, u_char *secp, lba_t lba)
64 {
65 	u_int cyl, hd, sec;
66 
67 	mkimg_chs(lba, 1023, &cyl, &hd, &sec);
68 	*cylp = cyl;
69 	*hdp = hd;
70 	*secp = (sec & 0x3f) | ((cyl >> 2) & 0xc0);
71 }
72 
73 static int
74 mbr_write(lba_t imgsz __unused, void *bootcode)
75 {
76 	u_char *mbr;
77 	struct dos_partition *dpbase, *dp;
78 	struct part *part;
79 	lba_t size;
80 	int error;
81 
82 	mbr = malloc(secsz);
83 	if (mbr == NULL)
84 		return (ENOMEM);
85 	if (bootcode != NULL) {
86 		memcpy(mbr, bootcode, DOSPARTOFF);
87 		memset(mbr + DOSPARTOFF, 0, secsz - DOSPARTOFF);
88 	} else
89 		memset(mbr, 0, secsz);
90 	le16enc(mbr + DOSMAGICOFFSET, DOSMAGIC);
91 	dpbase = (void *)(mbr + DOSPARTOFF);
92 	TAILQ_FOREACH(part, &partlist, link) {
93 		size = round_track(part->size);
94 		dp = dpbase + part->index;
95 		if (active_partition != 0)
96 			dp->dp_flag =
97 			    (part->index + 1 == active_partition) ? 0x80 : 0;
98 		else
99 			dp->dp_flag =
100 			    (part->index == 0 && bootcode != NULL) ? 0x80 : 0;
101 		mbr_chs(&dp->dp_scyl, &dp->dp_shd, &dp->dp_ssect,
102 		    part->block);
103 		dp->dp_typ = ALIAS_TYPE2INT(part->type);
104 		mbr_chs(&dp->dp_ecyl, &dp->dp_ehd, &dp->dp_esect,
105 		    part->block + size - 1);
106 		le32enc(&dp->dp_start, part->block);
107 		le32enc(&dp->dp_size, size);
108 	}
109 	error = image_write(0, mbr, 1);
110 	free(mbr);
111 	return (error);
112 }
113 
114 static struct mkimg_scheme mbr_scheme = {
115 	.name = "mbr",
116 	.description = "Master Boot Record",
117 	.aliases = mbr_aliases,
118 	.metadata = mbr_metadata,
119 	.write = mbr_write,
120 	.bootcode = 512,
121 	.nparts = NDOSPART,
122 	.maxsecsz = 4096
123 };
124 
125 SCHEME_DEFINE(mbr_scheme);
126