aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorroot <root>2007-09-10 09:53:07 +0000
committerroot <root>2007-09-10 09:53:07 +0000
commit529b3f6fffd9ed9742009162077ea5c98325d2a9 (patch)
treeedee67eb0b8f16c7b6cb00ee662c1a34ac885fa5
parent61c1751407b35ed4fe98d5e1e39608c7940349d2 (diff)
downloadgpt-529b3f6fffd9ed9742009162077ea5c98325d2a9.tar.gz
gpt-529b3f6fffd9ed9742009162077ea5c98325d2a9.tar.bz2
gpt-529b3f6fffd9ed9742009162077ea5c98325d2a9.zip
*** empty log message ***
-rw-r--r--src/Makefile.am7
-rw-r--r--src/add.c16
-rw-r--r--src/gpt.c18
-rw-r--r--src/gpt.h7
-rw-r--r--src/guid.c6
-rw-r--r--src/header.c73
-rw-r--r--src/modify.c37
-rw-r--r--src/prototypes.h58
-rw-r--r--src/show.c11
-rw-r--r--src/util.c39
10 files changed, 214 insertions, 58 deletions
diff --git a/src/Makefile.am b/src/Makefile.am
index ed73d14..86080d0 100644
--- a/src/Makefile.am
+++ b/src/Makefile.am
@@ -5,9 +5,12 @@
# Copyright (c) 2007 James McKenzie <james@fishsoup.dhs.org>,
# All rights reserved.
#
-# $Id: Makefile.am,v 1.3 2007/09/09 22:29:50 root Exp $
+# $Id: Makefile.am,v 1.4 2007/09/10 09:53:07 root Exp $
#
# $Log: Makefile.am,v $
+# Revision 1.4 2007/09/10 09:53:07 root
+# *** empty log message ***
+#
# Revision 1.3 2007/09/09 22:29:50 root
# *** empty log message ***
#
@@ -28,7 +31,7 @@ noinst_HEADERS=project.h prototypes.h
bin_PROGRAMS = gpt
-SRCS=gpt.c version.c util.c guid.c crc.c header.c disk.c pmbr.c show.c entry.c new.c add.c
+SRCS=gpt.c version.c util.c guid.c crc.c header.c disk.c pmbr.c show.c entry.c new.c modify.c
gpt_SOURCES = ${SRCS}
gpt_LDADD =
diff --git a/src/add.c b/src/add.c
deleted file mode 100644
index 9ffc852..0000000
--- a/src/add.c
+++ /dev/null
@@ -1,16 +0,0 @@
-#include "project.h"
-
-
-
-void
-add (DISK * d, int n, char *guid, int start, int end)
-{
- GPT_header *h;
- GPT_entry *e;
-
- uint8_t buf[512];
-
- disk_read (d, buf, 1, 1);
- h = (GPT_header *) buf;
-
-}
diff --git a/src/gpt.c b/src/gpt.c
index 2f4aea2..1b93dc1 100644
--- a/src/gpt.c
+++ b/src/gpt.c
@@ -6,10 +6,13 @@
*
*/
-static char rcsid[] = "$Id: gpt.c,v 1.4 2007/09/09 22:30:05 root Exp $";
+static char rcsid[] = "$Id: gpt.c,v 1.5 2007/09/10 09:53:07 root Exp $";
/*
* $Log: gpt.c,v $
+ * Revision 1.5 2007/09/10 09:53:07 root
+ * *** empty log message ***
+ *
* Revision 1.4 2007/09/09 22:30:05 root
* *** empty log message ***
*
@@ -28,6 +31,13 @@ static char rcsid[] = "$Id: gpt.c,v 1.4 2007/09/09 22:30:05 root Exp $";
#include "project.h"
+void usage(void)
+{
+fprintf(stderr,
+"Usage:\n"
+"gpt disk-device [-l] [-e n] [-m n
+
+
int
main (int argc, char *argv[])
@@ -35,11 +45,11 @@ main (int argc, char *argv[])
DISK *d;
- d = disk_open ("/dev/sdb");
+ d = disk_open ("/dev/sda");
- //new(d);
- add (d, 1, "linux-ext3", 10 * 1024 * 2, 30 * 1024 * 2);
+ new(d);
+ add (d, 1, "linux-boot","linux-ext3", 10 * 1024 * 2, 30 * 1024 * 2);
show (d);
#if 0
diff --git a/src/gpt.h b/src/gpt.h
index 2d1f263..bcadd31 100644
--- a/src/gpt.h
+++ b/src/gpt.h
@@ -81,3 +81,10 @@ typedef struct
MBR_entry entry[4];
uint8_t signature[2];
} PACKED MBR;
+
+
+typedef struct {
+ GPT_header header,alt_header;
+} GPT_headers;
+
+
diff --git a/src/guid.c b/src/guid.c
index a4b49be..31fbf00 100644
--- a/src/guid.c
+++ b/src/guid.c
@@ -66,9 +66,11 @@ a_to_guid (char *a, GUID * g)
while (ptr->name)
{
- if (!strcmp (a, ptr->name))
+ if (!strcmp (a, ptr->name)) {
memcpy (g, &ptr->g, sizeof (GUID));
- return 0;
+ return 0;
+ }
+ ptr++;
}
while ((*a) && (ndigits != 32))
diff --git a/src/header.c b/src/header.c
index 4e68934..6c3133d 100644
--- a/src/header.c
+++ b/src/header.c
@@ -116,3 +116,76 @@ header_show (DISK * d, GPT_header * h)
}
}
+
+int header_validate(DISK *d,GPT_header *h)
+{
+ GPT_header c = *h;
+uint32_t crc;
+
+ c.header_crc = 0;
+
+if (memcmp (h->signature, GPT_HEADER_SIGNATURE, sizeof (h->signature)))
+ return 0;
+
+ crc = crc32 (0, &c, sizeof (c));
+ if (crc != h->header_crc) return 0;
+
+
+ crc = header_calc_ent_crc (d, h);
+
+if (crc == h->partition_entry_crc) return 0;
+
+return 1;
+}
+
+void header_redo_ent_crc(DISK *d,GPT_header *h)
+{
+h->partition_entry_crc=header_calc_ent_crc(d,h);
+h->header_crc=0;
+h->header_crc=crc32(0,h,sizeof(*h));
+}
+void header_write(DISK *d,GPT_header *h)
+{
+uint8_t buf[512];
+memcpy(buf,h,sizeof(*h));
+
+printf("Writing header to lba %lld\n",(long long) h->my_lba);
+disk_write(d,buf,h->my_lba,1);
+}
+
+
+int headers_validate(DISK *d,GPT_headers *h)
+{
+if (!header_validate(d,&h->header)) return 0;
+return header_validate(d,&h->alt_header);
+}
+
+
+GPT_headers headers_get(DISK *d)
+{
+GPT_headers ret;
+uint8_t buf[512];
+uint64_t lbas;
+
+
+lbas=disk_lbas(d);
+
+disk_read(d,buf,1,1);
+memcpy(&ret.header,buf,sizeof(ret.header));
+
+lbas--;
+
+if (lbas!=ret.header.alternate_lba) {
+ fprintf(stderr,"WARNING: alternate lba is not at end of disk\n");
+}
+
+disk_read(d,buf,ret.header.alternate_lba,1);
+memcpy(&ret.alt_header,buf,sizeof(ret.alt_header));
+
+
+return ret;
+}
+
+
+
+
diff --git a/src/modify.c b/src/modify.c
new file mode 100644
index 0000000..7a26073
--- /dev/null
+++ b/src/modify.c
@@ -0,0 +1,37 @@
+#include "project.h"
+
+
+
+int
+add (DISK * d, int n, char *name,char *type_guid, int start, int end)
+{
+ GPT_headers h;
+ GPT_entry e={0};
+
+ h=headers_get(d);
+
+ if(a_to_guid(type_guid,&e.type)) {
+ fprintf(stderr,"GUID type %s was not parsed\n");
+ return -1;
+ }
+ e.label=guid_random();
+ e.start=start;
+ e.end=end;
+
+
+ ascii_to_utf16(name,-1,e.name,sizeof(e.name));
+
+
+ entry_show(&e);
+
+/*Odd order because efi says so*/
+
+ entry_write(d,&h.header,n,&e);
+ header_redo_ent_crc(d,&h.header);
+ header_write(d,&h.header);
+
+ entry_write(d,&h.alt_header,n,&e);
+ header_redo_ent_crc(d,&h.alt_header);
+ header_write(d,&h.alt_header);
+return 0;
+}
diff --git a/src/prototypes.h b/src/prototypes.h
index 6ad5bc8..c4b853a 100644
--- a/src/prototypes.h
+++ b/src/prototypes.h
@@ -1,38 +1,44 @@
/* gpt.c */
-int main (int argc, char *argv[]);
+int main(int argc, char *argv[]);
/* version.c */
/* util.c */
-void hexdump (FILE * f, uint8_t * data, int s, int l);
-void utf16_to_ascii (uint16_t * u16, int u16_len, uint8_t * u8, int u8_len);
+void hexdump(FILE *f, uint8_t *data, int s, int l);
+void utf16_to_ascii(void *_u16, int u16_len, void *_u8, int u8_len);
+void ascii_to_utf16(void *_u8, int u8_len, void *_u16, int u16_len);
/* guid.c */
-int guid_cmp (GUID * a, GUID * b);
-char *guid_to_a (GUID g);
-int a_to_guid (char *a, GUID * g);
-GUID guid_random (void);
+int guid_cmp(GUID *a, GUID *b);
+char *guid_to_a(GUID g);
+int a_to_guid(char *a, GUID *g);
+GUID guid_random(void);
/* crc.c */
-uint32_t crc32 (uint32_t crc, const void *_buf, int len);
+uint32_t crc32(uint32_t crc, const void *_buf, int len);
/* header.c */
-void header_calc_crc (GPT_header * h);
-GPT_header header_new (GUID disk_guid, int lbas, int alt);
-uint32_t header_calc_ent_crc (DISK * d, GPT_header * h);
-void header_show (DISK * d, GPT_header * h);
+void header_calc_crc(GPT_header *h);
+GPT_header header_new(GUID disk_guid, int lbas, int alt);
+uint32_t header_calc_ent_crc(DISK *d, GPT_header *h);
+void header_show(DISK *d, GPT_header *h);
+int header_validate(DISK *d, GPT_header *h);
+void header_redo_ent_crc(DISK *d, GPT_header *h);
+void header_write(DISK *d, GPT_header *h);
+int headers_validate(DISK *d, GPT_headers *h);
+GPT_headers headers_get(DISK *d);
/* disk.c */
-DISK *disk_open (char *fn);
-void disk_read (DISK * d, void *buf, uint64_t lba, int lbas);
-void disk_write (DISK * d, void *buf, uint64_t lba, int lbas);
-uint64_t disk_lbas (DISK * d);
+DISK *disk_open(char *fn);
+void disk_read(DISK *d, void *buf, uint64_t lba, int lbas);
+void disk_write(DISK *d, void *buf, uint64_t lba, int lbas);
+uint64_t disk_lbas(DISK *d);
/* pmbr.c */
-void mbr_entry_show (MBR_entry * e);
-void mbr_show (MBR * m);
-MBR mbr_new (uint64_t lbas);
+void mbr_entry_show(MBR_entry *e);
+void mbr_show(MBR *m);
+MBR mbr_new(uint64_t lbas);
/* show.c */
-void show (DISK * d);
+void show(DISK *d);
/* entry.c */
-void entry_write (DISK * d, GPT_header * h, int n, GPT_entry * e);
-GPT_entry entry_read (DISK * d, GPT_header * h, int n);
-int entry_empty (GPT_entry * e);
-void entry_show (GPT_entry * e);
+void entry_write(DISK *d, GPT_header *h, int n, GPT_entry *e);
+GPT_entry entry_read(DISK *d, GPT_header *h, int n);
+int entry_empty(GPT_entry *e);
+void entry_show(GPT_entry *e);
/* new.c */
-void new (DISK * d);
+void new(DISK *d);
/* add.c */
-void add (DISK * d, int n, char *guid, int start, int end);
+int add(DISK *d, int n, char *name, char *type_guid, int start, int end);
diff --git a/src/show.c b/src/show.c
index 443bd24..0047e97 100644
--- a/src/show.c
+++ b/src/show.c
@@ -6,7 +6,7 @@ void
show (DISK * d)
{
MBR *m;
- GPT_header *h;
+ GPT_headers h;
GPT_entry *e;
uint8_t buf[512];
@@ -15,12 +15,9 @@ show (DISK * d)
m = (MBR *) buf;
mbr_show (m);
- disk_read (d, buf, 1, 1);
- h = (GPT_header *) buf;
- header_show (d, h);
+ h=headers_get(d);
+ header_show (d, &h.header);
- disk_read (d, buf, disk_lbas (d) - 1, 1);
- h = (GPT_header *) buf;
- header_show (d, h);
+ header_show (d, &h.alt_header);
}
diff --git a/src/util.c b/src/util.c
index 104a16c..f59ccb9 100644
--- a/src/util.c
+++ b/src/util.c
@@ -53,8 +53,11 @@ hexdump (FILE * f, uint8_t * data, int s, int l)
}
void
-utf16_to_ascii (uint16_t * u16, int u16_len, uint8_t * u8, int u8_len)
+utf16_to_ascii (void * _u16, int u16_len, void * _u8, int u8_len)
{
+ uint16_t *u16=(uint16_t *) _u16;
+ uint8_t *u8=(uint8_t *) _u8;
+
u16_len >>= 1;
if (u8_len == 1)
@@ -78,3 +81,37 @@ utf16_to_ascii (uint16_t * u16, int u16_len, uint8_t * u8, int u8_len)
*u8 = 0;
return;
}
+
+void
+ascii_to_utf16 (void * _u8, int u8_len, void * _u16, int u16_len)
+{
+ uint16_t *u16=(uint16_t *) _u16;
+ uint8_t *u8=(uint8_t *) _u8;
+
+ u16_len >>= 1;
+
+ if (u16_len == 1)
+ {
+ *u16 = 0;
+ return;
+ }
+ while (*u8 && u8_len)
+ {
+ *(u16++) = *(u8++);
+ u16_len--;
+ u8_len--;
+
+ if (u16_len == 1)
+ {
+ *u16 = 0;
+ return;
+ }
+ }
+
+ *u16 = 0;
+ return;
+}
+
+
+
+