gpt.c (8d249f5e2faee1108a0e0c94b382dec45a77b973) | gpt.c (429971147386e142afa18df15e878df6735f364b) |
---|---|
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 --- 19 unchanged lines hidden (view full) --- 28__FBSDID("$FreeBSD$"); 29 30#include <sys/errno.h> 31#include <stddef.h> 32#include <stdint.h> 33#include <stdlib.h> 34#include <string.h> 35#include <unistd.h> | 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 --- 19 unchanged lines hidden (view full) --- 28__FBSDID("$FreeBSD$"); 29 30#include <sys/errno.h> 31#include <stddef.h> 32#include <stdint.h> 33#include <stdlib.h> 34#include <string.h> 35#include <unistd.h> |
36#include <uuid.h> | |
37 | 36 |
38#include <sys/disk/gpt.h> 39#include <sys/disk/mbr.h> | 37#include <gpt.h> 38#include <mbr.h> |
40 41#include "endian.h" 42#include "image.h" 43#include "mkimg.h" 44#include "scheme.h" 45 | 39 40#include "endian.h" 41#include "image.h" 42#include "mkimg.h" 43#include "scheme.h" 44 |
46static uuid_t gpt_uuid_efi = GPT_ENT_TYPE_EFI; 47static uuid_t gpt_uuid_freebsd = GPT_ENT_TYPE_FREEBSD; 48static uuid_t gpt_uuid_freebsd_boot = GPT_ENT_TYPE_FREEBSD_BOOT; 49static uuid_t gpt_uuid_freebsd_nandfs = GPT_ENT_TYPE_FREEBSD_NANDFS; 50static uuid_t gpt_uuid_freebsd_swap = GPT_ENT_TYPE_FREEBSD_SWAP; 51static uuid_t gpt_uuid_freebsd_ufs = GPT_ENT_TYPE_FREEBSD_UFS; 52static uuid_t gpt_uuid_freebsd_vinum = GPT_ENT_TYPE_FREEBSD_VINUM; 53static uuid_t gpt_uuid_freebsd_zfs = GPT_ENT_TYPE_FREEBSD_ZFS; 54static uuid_t gpt_uuid_mbr = GPT_ENT_TYPE_MBR; 55static uuid_t gpt_uuid_ms_basic_data = GPT_ENT_TYPE_MS_BASIC_DATA; | 45static mkimg_uuid_t gpt_uuid_efi = GPT_ENT_TYPE_EFI; 46static mkimg_uuid_t gpt_uuid_freebsd = GPT_ENT_TYPE_FREEBSD; 47static mkimg_uuid_t gpt_uuid_freebsd_boot = GPT_ENT_TYPE_FREEBSD_BOOT; 48static mkimg_uuid_t gpt_uuid_freebsd_nandfs = GPT_ENT_TYPE_FREEBSD_NANDFS; 49static mkimg_uuid_t gpt_uuid_freebsd_swap = GPT_ENT_TYPE_FREEBSD_SWAP; 50static mkimg_uuid_t gpt_uuid_freebsd_ufs = GPT_ENT_TYPE_FREEBSD_UFS; 51static mkimg_uuid_t gpt_uuid_freebsd_vinum = GPT_ENT_TYPE_FREEBSD_VINUM; 52static mkimg_uuid_t gpt_uuid_freebsd_zfs = GPT_ENT_TYPE_FREEBSD_ZFS; 53static mkimg_uuid_t gpt_uuid_mbr = GPT_ENT_TYPE_MBR; 54static mkimg_uuid_t gpt_uuid_ms_basic_data = GPT_ENT_TYPE_MS_BASIC_DATA; |
56 57static struct mkimg_alias gpt_aliases[] = { 58 { ALIAS_EFI, ALIAS_PTR2TYPE(&gpt_uuid_efi) }, 59 { ALIAS_FREEBSD, ALIAS_PTR2TYPE(&gpt_uuid_freebsd) }, 60 { ALIAS_FREEBSD_BOOT, ALIAS_PTR2TYPE(&gpt_uuid_freebsd_boot) }, 61 { ALIAS_FREEBSD_NANDFS, ALIAS_PTR2TYPE(&gpt_uuid_freebsd_nandfs) }, 62 { ALIAS_FREEBSD_SWAP, ALIAS_PTR2TYPE(&gpt_uuid_freebsd_swap) }, 63 { ALIAS_FREEBSD_UFS, ALIAS_PTR2TYPE(&gpt_uuid_freebsd_ufs) }, --- 57 unchanged lines hidden (view full) --- 121 const uint8_t *p = (const uint8_t *)buf; 122 uint32_t crc = ~0U; 123 124 while (sz--) 125 crc = crc32_tab[(crc ^ *p++) & 0xff] ^ (crc >> 8); 126 return (crc ^ ~0U); 127} 128 | 55 56static struct mkimg_alias gpt_aliases[] = { 57 { ALIAS_EFI, ALIAS_PTR2TYPE(&gpt_uuid_efi) }, 58 { ALIAS_FREEBSD, ALIAS_PTR2TYPE(&gpt_uuid_freebsd) }, 59 { ALIAS_FREEBSD_BOOT, ALIAS_PTR2TYPE(&gpt_uuid_freebsd_boot) }, 60 { ALIAS_FREEBSD_NANDFS, ALIAS_PTR2TYPE(&gpt_uuid_freebsd_nandfs) }, 61 { ALIAS_FREEBSD_SWAP, ALIAS_PTR2TYPE(&gpt_uuid_freebsd_swap) }, 62 { ALIAS_FREEBSD_UFS, ALIAS_PTR2TYPE(&gpt_uuid_freebsd_ufs) }, --- 57 unchanged lines hidden (view full) --- 120 const uint8_t *p = (const uint8_t *)buf; 121 uint32_t crc = ~0U; 122 123 while (sz--) 124 crc = crc32_tab[(crc ^ *p++) & 0xff] ^ (crc >> 8); 125 return (crc ^ ~0U); 126} 127 |
129static void 130gpt_uuid_enc(void *buf, const uuid_t *uuid) 131{ 132 uint8_t *p = buf; 133 int i; 134 135 le32enc(p, uuid->time_low); 136 le16enc(p + 4, uuid->time_mid); 137 le16enc(p + 6, uuid->time_hi_and_version); 138 p[8] = uuid->clock_seq_hi_and_reserved; 139 p[9] = uuid->clock_seq_low; 140 for (i = 0; i < _UUID_NODE_LEN; i++) 141 p[10 + i] = uuid->node[i]; 142} 143 | |
144static u_int 145gpt_tblsz(void) 146{ 147 u_int ents; 148 149 ents = secsz / sizeof(struct gpt_ent); 150 return ((nparts + ents - 1) / ents); 151} --- 37 unchanged lines hidden (view full) --- 189 error = image_write(0, pmbr, 1); 190 free(pmbr); 191 return (error); 192} 193 194static struct gpt_ent * 195gpt_mktbl(u_int tblsz) 196{ | 128static u_int 129gpt_tblsz(void) 130{ 131 u_int ents; 132 133 ents = secsz / sizeof(struct gpt_ent); 134 return ((nparts + ents - 1) / ents); 135} --- 37 unchanged lines hidden (view full) --- 173 error = image_write(0, pmbr, 1); 174 free(pmbr); 175 return (error); 176} 177 178static struct gpt_ent * 179gpt_mktbl(u_int tblsz) 180{ |
197 uuid_t uuid; | 181 mkimg_uuid_t uuid; |
198 struct gpt_ent *tbl, *ent; 199 struct part *part; 200 int c, idx; 201 202 tbl = calloc(tblsz, secsz); 203 if (tbl == NULL) 204 return (NULL); 205 206 TAILQ_FOREACH(part, &partlist, link) { 207 ent = tbl + part->index; | 182 struct gpt_ent *tbl, *ent; 183 struct part *part; 184 int c, idx; 185 186 tbl = calloc(tblsz, secsz); 187 if (tbl == NULL) 188 return (NULL); 189 190 TAILQ_FOREACH(part, &partlist, link) { 191 ent = tbl + part->index; |
208 gpt_uuid_enc(&ent->ent_type, ALIAS_TYPE2PTR(part->type)); | 192 mkimg_uuid_enc(&ent->ent_type, ALIAS_TYPE2PTR(part->type)); |
209 mkimg_uuid(&uuid); | 193 mkimg_uuid(&uuid); |
210 gpt_uuid_enc(&ent->ent_uuid, &uuid); | 194 mkimg_uuid_enc(&ent->ent_uuid, &uuid); |
211 le64enc(&ent->ent_lba_start, part->block); 212 le64enc(&ent->ent_lba_end, part->block + part->size - 1); 213 if (part->label != NULL) { 214 idx = 0; 215 while ((c = part->label[idx]) != '\0') { 216 le16enc(ent->ent_name + idx, c); 217 idx++; 218 } --- 14 unchanged lines hidden (view full) --- 233 crc = crc32(hdr, offsetof(struct gpt_hdr, padding)); 234 le64enc(&hdr->hdr_crc_self, crc); 235 return (image_write(self, hdr, 1)); 236} 237 238static int 239gpt_write(lba_t imgsz, void *bootcode) 240{ | 195 le64enc(&ent->ent_lba_start, part->block); 196 le64enc(&ent->ent_lba_end, part->block + part->size - 1); 197 if (part->label != NULL) { 198 idx = 0; 199 while ((c = part->label[idx]) != '\0') { 200 le16enc(ent->ent_name + idx, c); 201 idx++; 202 } --- 14 unchanged lines hidden (view full) --- 217 crc = crc32(hdr, offsetof(struct gpt_hdr, padding)); 218 le64enc(&hdr->hdr_crc_self, crc); 219 return (image_write(self, hdr, 1)); 220} 221 222static int 223gpt_write(lba_t imgsz, void *bootcode) 224{ |
241 uuid_t uuid; | 225 mkimg_uuid_t uuid; |
242 struct gpt_ent *tbl; 243 struct gpt_hdr *hdr; 244 uint32_t crc; 245 u_int tblsz; 246 int error; 247 248 /* PMBR */ 249 error = gpt_write_pmbr(imgsz, bootcode); --- 20 unchanged lines hidden (view full) --- 270 } 271 memset(hdr, 0, secsz); 272 memcpy(hdr->hdr_sig, GPT_HDR_SIG, sizeof(hdr->hdr_sig)); 273 le32enc(&hdr->hdr_revision, GPT_HDR_REVISION); 274 le32enc(&hdr->hdr_size, offsetof(struct gpt_hdr, padding)); 275 le64enc(&hdr->hdr_lba_start, 2 + tblsz); 276 le64enc(&hdr->hdr_lba_end, imgsz - tblsz - 2); 277 mkimg_uuid(&uuid); | 226 struct gpt_ent *tbl; 227 struct gpt_hdr *hdr; 228 uint32_t crc; 229 u_int tblsz; 230 int error; 231 232 /* PMBR */ 233 error = gpt_write_pmbr(imgsz, bootcode); --- 20 unchanged lines hidden (view full) --- 254 } 255 memset(hdr, 0, secsz); 256 memcpy(hdr->hdr_sig, GPT_HDR_SIG, sizeof(hdr->hdr_sig)); 257 le32enc(&hdr->hdr_revision, GPT_HDR_REVISION); 258 le32enc(&hdr->hdr_size, offsetof(struct gpt_hdr, padding)); 259 le64enc(&hdr->hdr_lba_start, 2 + tblsz); 260 le64enc(&hdr->hdr_lba_end, imgsz - tblsz - 2); 261 mkimg_uuid(&uuid); |
278 gpt_uuid_enc(&hdr->hdr_uuid, &uuid); | 262 mkimg_uuid_enc(&hdr->hdr_uuid, &uuid); |
279 le32enc(&hdr->hdr_entries, nparts); 280 le32enc(&hdr->hdr_entsz, sizeof(struct gpt_ent)); 281 crc = crc32(tbl, nparts * sizeof(struct gpt_ent)); 282 le32enc(&hdr->hdr_crc_table, crc); 283 error = gpt_write_hdr(hdr, 1, imgsz - 1, 2); 284 if (!error) 285 error = gpt_write_hdr(hdr, imgsz - 1, 1, imgsz - tblsz - 1); 286 free(hdr); --- 19 unchanged lines hidden --- | 263 le32enc(&hdr->hdr_entries, nparts); 264 le32enc(&hdr->hdr_entsz, sizeof(struct gpt_ent)); 265 crc = crc32(tbl, nparts * sizeof(struct gpt_ent)); 266 le32enc(&hdr->hdr_crc_table, crc); 267 error = gpt_write_hdr(hdr, 1, imgsz - 1, 2); 268 if (!error) 269 error = gpt_write_hdr(hdr, imgsz - 1, 1, imgsz - tblsz - 1); 270 free(hdr); --- 19 unchanged lines hidden --- |