ahci - Add quirks for Marvell devices
[dragonfly.git] / sbin / gpt / boot.c
blobf5dae3644b3f4ddc3993f5e5ce903b1d403e4885
1 /*
2 * Copyright (c) 2008 The DragonFly Project. All rights reserved.
3 *
4 * This code is derived from software contributed to The DragonFly Project
5 * by Matthew Dillon <dillon@backplane.com>
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in
15 * the documentation and/or other materials provided with the
16 * distribution.
17 * 3. Neither the name of The DragonFly Project nor the names of its
18 * contributors may be used to endorse or promote products derived
19 * from this software without specific, prior written permission.
21 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
24 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
25 * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
26 * INCIDENTAL, SPECIAL, EXEMPLARY OR CONSEQUENTIAL DAMAGES (INCLUDING,
27 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
28 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
29 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
30 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
31 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32 * SUCH DAMAGE.
34 * $DragonFly: src/sbin/gpt/boot.c,v 1.2 2008/08/21 23:10:04 thomas Exp $
37 #include <sys/types.h>
39 #include <err.h>
40 #include <stddef.h>
41 #include <stdio.h>
42 #include <stdlib.h>
43 #include <string.h>
44 #include <unistd.h>
45 #include <fcntl.h>
47 #include "map.h"
48 #include "gpt.h"
50 static void
51 usage_boot(void)
53 fprintf(stderr, "usage: %s device\n", getprogname());
54 exit(1);
57 static void
58 bootset(int fd)
60 uuid_t uuid;
61 off_t block;
62 off_t size;
63 unsigned int entry;
64 map_t *gpt, *tpg;
65 map_t *tbl, *lbt;
66 map_t *map;
67 u_int32_t status;
68 struct gpt_hdr *hdr;
69 struct gpt_ent *ent;
70 struct mbr *mbr;
71 int bfd;
74 * Paramters for boot partition
76 uuid_name_lookup(&uuid, "DragonFly Label32", &status);
77 if (status != uuid_s_ok)
78 err(1, "unable to find uuid for 'DragonFly Label32'");
79 entry = 0;
80 block = 0;
81 size = (off_t)1024 * 1024 * 1024 / 512; /* 1GB */
83 gpt = map_find(MAP_TYPE_PRI_GPT_HDR);
84 if (gpt == NULL)
85 errx(1, "%s: error: no primary GPT header", device_name);
86 tpg = map_find(MAP_TYPE_SEC_GPT_HDR);
87 if (tpg == NULL)
88 errx(1, "%s: error: no secondary GPT header", device_name);
89 tbl = map_find(MAP_TYPE_PRI_GPT_TBL);
90 lbt = map_find(MAP_TYPE_SEC_GPT_TBL);
91 if (tbl == NULL || lbt == NULL) {
92 errx(1, "%s: error: no primary or secondary gpt table",
93 device_name);
96 hdr = gpt->map_data;
97 if (entry > le32toh(hdr->hdr_entries)) {
98 errx(1, "%s: error: index %u out of range (%u max)",
99 device_name, entry, le32toh(hdr->hdr_entries));
102 ent = (void *)((char *)tbl->map_data + entry *
103 le32toh(hdr->hdr_entsz));
104 if (!uuid_is_nil(&ent->ent_type, NULL)) {
105 errx(1, "%s: error: entry at index %d is not free",
106 device_name, entry);
108 map = map_alloc(block, size);
109 if (map == NULL)
110 errx(1, "%s: error: no space available on device", device_name);
111 block = map->map_start;
112 size = map->map_size;
114 le_uuid_enc(&ent->ent_type, &uuid);
115 ent->ent_lba_start = htole64(map->map_start);
116 ent->ent_lba_end = htole64(map->map_start + map->map_size - 1LL);
118 hdr->hdr_crc_table = htole32(crc32(tbl->map_data,
119 le32toh(hdr->hdr_entries) *
120 le32toh(hdr->hdr_entsz)));
121 hdr->hdr_crc_self = 0;
122 hdr->hdr_crc_self = htole32(crc32(hdr, le32toh(hdr->hdr_size)));
124 gpt_write(fd, gpt);
125 gpt_write(fd, tbl);
127 hdr = tpg->map_data;
128 ent = (void*)((char*)lbt->map_data + entry * le32toh(hdr->hdr_entsz));
129 le_uuid_enc(&ent->ent_type, &uuid);
130 ent->ent_lba_start = htole64(map->map_start);
131 ent->ent_lba_end = htole64(map->map_start + map->map_size - 1LL);
133 hdr->hdr_crc_table = htole32(crc32(lbt->map_data,
134 le32toh(hdr->hdr_entries) *
135 le32toh(hdr->hdr_entsz)));
136 hdr->hdr_crc_self = 0;
137 hdr->hdr_crc_self = htole32(crc32(hdr, le32toh(hdr->hdr_size)));
139 gpt_write(fd, lbt);
140 gpt_write(fd, tpg);
143 * Create a dummy partition
145 map = map_find(MAP_TYPE_PMBR);
146 if (map == NULL)
147 errx(1, "I can't find the PMBR!");
148 mbr = map->map_data;
149 if (mbr == NULL)
150 errx(1, "I can't find the PMBR's data!");
153 * Copy in real boot code
155 bfd = open("/boot/boot0", O_RDONLY);
156 if (bfd < 0 ||
157 read(bfd, mbr->mbr_code, sizeof(mbr->mbr_code)) !=
158 sizeof(mbr->mbr_code)) {
159 errx(1, "Cannot read /boot/boot0");
161 close(bfd);
164 * Generate partition #1
166 mbr->mbr_part[1].part_shd = 0xff;
167 mbr->mbr_part[1].part_ssect = 0xff;
168 mbr->mbr_part[1].part_scyl = 0xff;
169 mbr->mbr_part[1].part_ehd = 0xff;
170 mbr->mbr_part[1].part_esect = 0xff;
171 mbr->mbr_part[1].part_ecyl = 0xff;
172 mbr->mbr_part[1].part_start_lo = htole16(block);
173 mbr->mbr_part[1].part_start_hi = htole16((block) >> 16);
174 mbr->mbr_part[1].part_size_lo = htole16(size);
175 mbr->mbr_part[1].part_size_hi = htole16(size >> 16);
177 mbr->mbr_part[1].part_typ = 165;
178 mbr->mbr_part[1].part_flag = 0x80;
180 gpt_write(fd, map);
184 cmd_boot(int argc, char *argv[])
186 int ch, fd;
188 while ((ch = getopt(argc, argv, "")) != -1) {
189 switch(ch) {
190 default:
191 usage_boot();
195 if (argc == optind)
196 usage_boot();
198 while (optind < argc) {
199 fd = gpt_open(argv[optind++]);
200 if (fd == -1) {
201 warn("unable to open device '%s'", device_name);
202 continue;
204 bootset(fd);
205 gpt_close(fd);
207 return (0);