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
35 #include <sys/types.h>
51 fprintf(stderr
, "usage: %s device\n", getprogname());
72 * Paramters for boot partition
74 uuid_name_lookup(&uuid
, "DragonFly Label32", &status
);
75 if (status
!= uuid_s_ok
)
76 err(1, "unable to find uuid for 'DragonFly Label32'");
79 size
= (off_t
)1024 * 1024 * 1024 / 512; /* 1GB */
81 gpt
= map_find(MAP_TYPE_PRI_GPT_HDR
);
83 errx(1, "%s: error: no primary GPT header", device_name
);
84 tpg
= map_find(MAP_TYPE_SEC_GPT_HDR
);
86 errx(1, "%s: error: no secondary GPT header", device_name
);
87 tbl
= map_find(MAP_TYPE_PRI_GPT_TBL
);
88 lbt
= map_find(MAP_TYPE_SEC_GPT_TBL
);
89 if (tbl
== NULL
|| lbt
== NULL
) {
90 errx(1, "%s: error: no primary or secondary gpt table",
95 if (entry
> le32toh(hdr
->hdr_entries
)) {
96 errx(1, "%s: error: index %u out of range (%u max)",
97 device_name
, entry
, le32toh(hdr
->hdr_entries
));
100 ent
= (void *)((char *)tbl
->map_data
+ entry
*
101 le32toh(hdr
->hdr_entsz
));
102 if (!uuid_is_nil(&ent
->ent_type
, NULL
)) {
103 errx(1, "%s: error: entry at index %d is not free",
106 map
= map_alloc(block
, size
);
108 errx(1, "%s: error: no space available on device", device_name
);
109 block
= map
->map_start
;
110 size
= map
->map_size
;
112 le_uuid_enc(&ent
->ent_type
, &uuid
);
113 ent
->ent_lba_start
= htole64(map
->map_start
);
114 ent
->ent_lba_end
= htole64(map
->map_start
+ map
->map_size
- 1LL);
116 hdr
->hdr_crc_table
= htole32(crc32(tbl
->map_data
,
117 le32toh(hdr
->hdr_entries
) *
118 le32toh(hdr
->hdr_entsz
)));
119 hdr
->hdr_crc_self
= 0;
120 hdr
->hdr_crc_self
= htole32(crc32(hdr
, le32toh(hdr
->hdr_size
)));
126 ent
= (void*)((char*)lbt
->map_data
+ entry
* le32toh(hdr
->hdr_entsz
));
127 le_uuid_enc(&ent
->ent_type
, &uuid
);
128 ent
->ent_lba_start
= htole64(map
->map_start
);
129 ent
->ent_lba_end
= htole64(map
->map_start
+ map
->map_size
- 1LL);
131 hdr
->hdr_crc_table
= htole32(crc32(lbt
->map_data
,
132 le32toh(hdr
->hdr_entries
) *
133 le32toh(hdr
->hdr_entsz
)));
134 hdr
->hdr_crc_self
= 0;
135 hdr
->hdr_crc_self
= htole32(crc32(hdr
, le32toh(hdr
->hdr_size
)));
141 * Create a dummy partition
143 map
= map_find(MAP_TYPE_PMBR
);
145 errx(1, "I can't find the PMBR!");
148 errx(1, "I can't find the PMBR's data!");
151 * Copy in real boot code
153 bfd
= open("/boot/boot0", O_RDONLY
);
155 read(bfd
, mbr
->mbr_code
, sizeof(mbr
->mbr_code
)) !=
156 sizeof(mbr
->mbr_code
)) {
157 errx(1, "Cannot read /boot/boot0");
162 * Generate partition #1
164 mbr
->mbr_part
[1].part_shd
= 0xff;
165 mbr
->mbr_part
[1].part_ssect
= 0xff;
166 mbr
->mbr_part
[1].part_scyl
= 0xff;
167 mbr
->mbr_part
[1].part_ehd
= 0xff;
168 mbr
->mbr_part
[1].part_esect
= 0xff;
169 mbr
->mbr_part
[1].part_ecyl
= 0xff;
170 mbr
->mbr_part
[1].part_start_lo
= htole16(block
);
171 mbr
->mbr_part
[1].part_start_hi
= htole16((block
) >> 16);
172 mbr
->mbr_part
[1].part_size_lo
= htole16(size
);
173 mbr
->mbr_part
[1].part_size_hi
= htole16(size
>> 16);
175 mbr
->mbr_part
[1].part_typ
= 108;
176 mbr
->mbr_part
[1].part_flag
= 0x80;
182 cmd_boot(int argc
, char *argv
[])
186 while ((ch
= getopt(argc
, argv
, "")) != -1) {
196 while (optind
< argc
) {
197 fd
= gpt_open(argv
[optind
++]);
199 warn("unable to open device '%s'", device_name
);