aboutsummaryrefslogtreecommitdiffstats
path: root/src/header.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/header.c')
-rw-r--r--src/header.c137
1 files changed, 107 insertions, 30 deletions
diff --git a/src/header.c b/src/header.c
index d577837..e92fd5d 100644
--- a/src/header.c
+++ b/src/header.c
@@ -1,36 +1,113 @@
#include "project.h"
-void header_print(uint8_t *buf)
+
+#define TABLE_SIZE (((sizeof(GPT_entry)*GPT_PARITION_ENTRIES)/SECTOR_SIZE)+2)
+
+void
+header_calc_crc (GPT_header * h)
{
-GPT_header *h=(GPT_header *) buf;
-
-
-printf ("Signature %c%c%c%c%c%c%c%c\n",
- h->signature[0],
- h->signature[1],
- h->signature[2],
- h->signature[3],
- h->signature[4],
- h->signature[5],
- h->signature[6],
- h->signature[7]);
-
-printf("rev=0x%08x header_size=%d header_crc=0x%08x\n",
- h->revision,h->header_size,h->header_crc);
-printf("my_lba=%lld alternate_lba=%lld first_lba=%lld last_lba=%lld\n",
- (long long) h->my_lba, (long long) h->alternate_lba,
- (long long) h->first_usable_lba,(long long) h->last_usable_lba);
-
-printf("guid=%s\n",guid_to_a(h->disk_guid));
-printf("partition_entry_lba=%lld n_entries=%d ent_size=%d ents_crc=%08x\n",
- (long long) h->partition_entry_lba, h->n_partition_entries,
- h->partition_entry_size,h->partition_entry_crc);
-
-printf("%x\n",crc32(0,buf,92));
-h->header_crc=0;
-printf("%x\n",crc32(0,buf,92));
-h->partition_entry_crc=0;
-printf("%x\n",crc32(0,buf,92));
+
+ h->header_crc = 0;
+ h->header_crc = crc32 (0, h, sizeof (GPT_header));
}
+GPT_header
+header_new (GUID disk_guid, int lbas, int alt)
+{
+ GPT_header ret = { 0 };
+
+ memcpy (ret.signature, GPT_HEADER_SIGNATURE, sizeof (ret.signature));
+ ret.revision = GPT_HEADER_REVISION_EFI10;
+ ret.header_size = sizeof (GPT_header);
+
+ ret.first_usable_lba = TABLE_SIZE;
+ ret.last_usable_lba = lbas - TABLE_SIZE;
+
+ ret.n_partition_entries = GPT_PARITION_ENTRIES;
+ ret.partition_entry_size = sizeof (GPT_entry);
+
+ ret.disk_guid = disk_guid;
+
+ if (!alt)
+ {
+ ret.my_lba = 1;
+ ret.alternate_lba = lbas - 1;
+ ret.partition_entry_lba = 2;
+ }
+ else
+ {
+ ret.my_lba = lbas - 1;
+ ret.alternate_lba = 1;
+ ret.partition_entry_lba = lbas - TABLE_SIZE;
+ }
+
+ header_calc_crc (&ret);
+
+ return ret;
+}
+
+
+
+uint32_t
+header_calc_ent_crc (DISK * d, GPT_header * h)
+{
+ int i;
+ GPT_entry e;
+ uint32_t crc = 0;
+
+
+ for (i = 0; i < h->n_partition_entries; ++i)
+ {
+ e=entry_read(d,h,i);
+ crc = crc32 (crc, &e, h->partition_entry_size);
+ }
+
+ return crc;
+}
+
+
+void
+header_show (DISK *d,GPT_header *h)
+{
+ GPT_header c=*h;
+ uint32_t crc;
+ int i;
+
+ if (h->my_lba<h->alternate_lba) {
+ printf(" GPT:\n");
+ } else {
+ printf(" ALTERNATE GPT:\n");
+ }
+
+ c.header_crc=0;
+ crc=crc32(0,&c,sizeof(c));
+
+ printf(" Signature %s, CRC %s\n",
+ (memcmp(h->signature, GPT_HEADER_SIGNATURE,sizeof(h->signature))) ? "INVALID":"valid",
+ (crc==h->header_crc) ?"matches":"DOES NOT MATCH");
+
+ printf (" rev=0x%08x lba=%lld alternate=%lld\n",
+ h->revision,
+ (long long) h->my_lba, (long long) h->alternate_lba);
+ printf (" usable lbas %lld-%lld\n",
+ (long long) h->first_usable_lba, (long long) h->last_usable_lba);
+
+ printf (" DISK GUID: %s\n", guid_to_a (h->disk_guid));
+
+
+ crc=header_calc_ent_crc(d,h);
+
+ printf (" patitions (at lba %lld) CRC %s:\n",
+ (long long) h->partition_entry_lba,
+ (crc == h->partition_entry_crc) ? "matches":"DOES NOT MATCH");
+
+ for (i=0;i<h->n_partition_entries;++i) {
+ GPT_entry e=entry_read(d,h,i);
+ if (!entry_empty(&e)) {
+ printf(" %d:\n",i);
+ entry_show(&e);
+ }
+ }
+
+}