2 * Copyright (c) 2008 The DragonFly Project. All rights reserved.
4 * This code is derived from software contributed to The DragonFly Project
5 * by Matthew Dillon <dillon@backplane.com>
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
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
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
34 * $DragonFly: src/sbin/gpt/boot.c,v 1.2 2008/08/21 23:10:04 thomas Exp $
37 #include <sys/types.h>
53 fprintf(stderr
, "usage: %s device\n", getprogname());
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'");
81 size
= (off_t
)1024 * 1024 * 1024 / 512; /* 1GB */
83 gpt
= map_find(MAP_TYPE_PRI_GPT_HDR
);
85 errx(1, "%s: error: no primary GPT header", device_name
);
86 tpg
= map_find(MAP_TYPE_SEC_GPT_HDR
);
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",
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",
108 map
= map_alloc(block
, size
);
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
)));
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
)));
143 * Create a dummy partition
145 map
= map_find(MAP_TYPE_PMBR
);
147 errx(1, "I can't find the PMBR!");
150 errx(1, "I can't find the PMBR's data!");
153 * Copy in real boot code
155 bfd
= open("/boot/boot0", O_RDONLY
);
157 read(bfd
, mbr
->mbr_code
, sizeof(mbr
->mbr_code
)) !=
158 sizeof(mbr
->mbr_code
)) {
159 errx(1, "Cannot read /boot/boot0");
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;
184 cmd_boot(int argc
, char *argv
[])
188 while ((ch
= getopt(argc
, argv
, "")) != -1) {
198 while (optind
< argc
) {
199 fd
= gpt_open(argv
[optind
++]);
201 warn("unable to open device '%s'", device_name
);