summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorChristophe Grenier <grenier@cgsecurity.org>2008-06-30 07:54:30 +0200
committerChristophe Grenier <grenier@cgsecurity.org>2008-06-30 07:54:30 +0200
commitab41e37ba5fde8fa63dfaa9b898b2c9931156333 (patch)
tree2024a567b22eda7b9c70479f83dbfd39a3fc756c
parentdf529b5d88450df0e8c3c4834bd8b20c04714b02 (diff)
Users can change the EFI partition type and the filesystem type for GPT
-rw-r--r--src/chgtype.c156
-rw-r--r--src/partgpt.c57
-rw-r--r--src/partgpt.h27
3 files changed, 190 insertions, 50 deletions
diff --git a/src/chgtype.c b/src/chgtype.c
index af7e998..006b052 100644
--- a/src/chgtype.c
+++ b/src/chgtype.c
@@ -38,11 +38,14 @@
#include "chgtype.h"
#include "log.h"
#include "guid_cmp.h"
+#include "guid_cpy.h"
+#include "partgpt.h"
extern const arch_fnct_t arch_gpt;
extern const arch_fnct_t arch_none;
extern const arch_fnct_t arch_i386;
extern const arch_fnct_t arch_sun;
+extern const struct systypes_gtp gpt_sys_types[];
struct part_name_struct
{
@@ -52,6 +55,8 @@ struct part_name_struct
static void change_part_type_cli(const disk_t *disk_car,partition_t *partition, char **current_cmd)
{
+ if(partition->arch->set_part_type==NULL)
+ return ;
while(*current_cmd[0]==',')
(*current_cmd)++;
{
@@ -78,6 +83,8 @@ static void change_part_type_ncurses(const disk_t *disk_car,partition_t *partiti
{ 'Q', "Proceed","Go set the partition type"},
{ 0, NULL, NULL }
};
+ if(partition->arch->set_part_type==NULL)
+ return ;
/* Create an index of all partition type except Intel extended */
new_partition=partition_new(NULL);
dup_partition_t(new_partition,partition);
@@ -132,6 +139,8 @@ static void change_part_type_ncurses2(const disk_t *disk_car, partition_t *parti
unsigned int i;
unsigned int current_element_num=0;
struct part_name_struct part_name[0x100];
+ if(partition->arch->set_part_type==NULL)
+ return ;
aff_copy(stdscr);
wmove(stdscr,4,0);
aff_part(stdscr, AFF_PART_ORDER|AFF_PART_STATUS, disk_car, partition);
@@ -253,22 +262,158 @@ static void change_part_type_ncurses2(const disk_t *disk_car, partition_t *parti
}
}
}
+
+static void gpt_change_part_type(const disk_t *disk_car, partition_t *partition)
+{
+ unsigned int offset=0;
+ unsigned int i,j;
+ unsigned int current_element_num=0;
+ log_info("gpt_change_part_type\n");
+ aff_copy(stdscr);
+ wmove(stdscr,4,0);
+ aff_part(stdscr, AFF_PART_ORDER|AFF_PART_STATUS, disk_car, partition);
+ wmove(stdscr,INTER_CHGTYPE_Y, INTER_CHGTYPE_X);
+ wattrset(stdscr, A_REVERSE);
+ wprintw(stdscr, "[ Proceed ]");
+ wattroff(stdscr, A_REVERSE);
+ while(1)
+ {
+ wmove(stdscr,5,0);
+ wprintw(stdscr, "Please choose the partition type, press Enter when done.");
+ wmove(stdscr,5+1,1);
+ wclrtoeol(stdscr);
+ if(offset>0)
+ wprintw(stdscr, "Previous");
+ for(i=offset;gpt_sys_types[i].name!=NULL && (i-offset)<3*INTER_CHGTYPE;i++)
+ {
+ if(i-offset<INTER_CHGTYPE)
+ wmove(stdscr,5+2+i-offset,0);
+ else if(i-offset<2*INTER_CHGTYPE)
+ wmove(stdscr,5+2+i-offset-INTER_CHGTYPE,26);
+ else
+ wmove(stdscr,5+2+i-offset-2*INTER_CHGTYPE,52);
+ wclrtoeol(stdscr); /* before addstr for BSD compatibility */
+ if(i==current_element_num)
+ {
+ wattrset(stdscr, A_REVERSE);
+ wprintw(stdscr,"%s", gpt_sys_types[i].name);
+ wattroff(stdscr, A_REVERSE);
+ } else
+ {
+ wprintw(stdscr,"%s", gpt_sys_types[i].name);
+ }
+ }
+ if(i-offset<INTER_CHGTYPE)
+ wmove(stdscr,5+2+i-offset,1);
+ else if(i-offset<2*INTER_CHGTYPE)
+ wmove(stdscr,5+2+i-offset-INTER_CHGTYPE,27);
+ else
+ wmove(stdscr,5+2+i-offset-2*INTER_CHGTYPE,53);
+ wclrtoeol(stdscr);
+ if(gpt_sys_types[i].name!=NULL)
+ wprintw(stdscr, "Next");
+ switch(wgetch(stdscr))
+ {
+ case 'p':
+ case 'P':
+ case KEY_UP:
+ if(current_element_num>0)
+ current_element_num--;
+ if(current_element_num<offset)
+ offset=current_element_num;
+ break;
+ case 'n':
+ case 'N':
+ case KEY_DOWN:
+ if(gpt_sys_types[current_element_num].name!=NULL && gpt_sys_types[current_element_num+1].name!=NULL)
+ current_element_num++;
+ if(current_element_num >= offset+3*INTER_CHGTYPE)
+ offset++;
+ break;
+ case KEY_LEFT:
+ if(current_element_num > INTER_CHGTYPE)
+ current_element_num-=INTER_CHGTYPE;
+ else
+ current_element_num=0;
+ if(current_element_num < offset)
+ offset=current_element_num;
+ break;
+ case KEY_PPAGE:
+ if(current_element_num > 3*INTER_CHGTYPE-1)
+ current_element_num-=3*INTER_CHGTYPE-1;
+ else
+ current_element_num=0;
+ if(current_element_num < offset)
+ offset=current_element_num;
+ break;
+ case KEY_RIGHT:
+ for(j=0;j<INTER_CHGTYPE;j++)
+ {
+ if(gpt_sys_types[current_element_num].name!=NULL && gpt_sys_types[current_element_num+1].name!=NULL)
+ current_element_num++;
+ if(current_element_num >= offset+3*INTER_CHGTYPE)
+ offset++;
+ }
+ break;
+ case KEY_NPAGE:
+ for(j=0;j<3*INTER_CHGTYPE;j++)
+ {
+ if(gpt_sys_types[current_element_num].name!=NULL && gpt_sys_types[current_element_num+1].name!=NULL)
+ current_element_num++;
+ if(current_element_num >= offset+3*INTER_CHGTYPE)
+ offset++;
+ }
+ break;
+ case 'Q':
+ case 'q':
+ case key_CR:
+#ifdef PADENTER
+ case PADENTER:
+#endif
+ guid_cpy(&partition->part_type_gpt, &gpt_sys_types[current_element_num].part_type);
+ return;
+ }
+ }
+}
#endif
void change_part_type(const disk_t *disk_car,partition_t *partition, char **current_cmd)
{
- const arch_fnct_t *arch=partition->arch;
if(partition->arch==NULL)
{
log_error("change_part_type arch==NULL\n");
return;
}
if(partition->arch==&arch_gpt)
+ {
+ if(*current_cmd!=NULL)
+ { /* TODO: implement it */
+ }
+ else
+ {
+#ifdef HAVE_NCURSES
+ gpt_change_part_type(disk_car, partition);
+#endif
+ }
+ log_info("Change partition type:\n");
+ log_partition(disk_car,partition);
partition->arch=&arch_none;
+ if(*current_cmd!=NULL)
+ change_part_type_cli(disk_car, partition, current_cmd);
+ else
+ {
+#ifdef HAVE_NCURSES
+ change_part_type_ncurses2(disk_car, partition);
+#endif
+ }
+ log_info("Change partition type:\n");
+ log_partition(disk_car,partition);
+ partition->arch=&arch_gpt;
+ return ;
+ }
if(partition->arch->set_part_type==NULL)
{
log_error("change_part_type set_part_type==NULL\n");
- partition->arch=arch;
return;
}
if(*current_cmd!=NULL)
@@ -282,13 +427,6 @@ void change_part_type(const disk_t *disk_car,partition_t *partition, char **curr
change_part_type_ncurses2(disk_car, partition);
#endif
}
- if(arch==&arch_gpt)
- {
- partition->arch=arch;
- /* TODO: use upart_type to set part_type_gpt */
- if(guid_cmp(partition->part_type_gpt, GPT_ENT_TYPE_UNUSED)==0)
- partition->part_type_gpt=GPT_ENT_TYPE_MS_BASIC_DATA;
- }
log_info("Change partition type:\n");
log_partition(disk_car,partition);
}
diff --git a/src/partgpt.c b/src/partgpt.c
index d96c86e..621595e 100644
--- a/src/partgpt.c
+++ b/src/partgpt.c
@@ -79,17 +79,8 @@ static void init_structure_gpt(const disk_t *disk_car,list_part_t *list_part, co
static const char *get_partition_typename_gpt(const partition_t *partition);
static const char *get_gpt_typename(const efi_guid_t part_type_gpt);
static void efi_generate_uuid(efi_guid_t *ent_uuid);
-#if 0
-static int set_part_type_gpt(partition_t *partition, efi_guid_t part_type_gpt);
-static efi_guid_t get_part_type_gpt(const partition_t *partition);
-#endif
-
-struct systypes_gtp {
- const efi_guid_t part_type;
- const char *name;
-};
-static const struct systypes_gtp gpt_sys_types[] = {
+const struct systypes_gtp gpt_sys_types[] = {
{ GPT_ENT_TYPE_EFI, "EFI System" },
{ GPT_ENT_TYPE_MBR, "MBR" },
{ GPT_ENT_TYPE_FREEBSD, "FreeBSD" },
@@ -147,13 +138,10 @@ arch_fnct_t arch_gpt=
.set_next_status=set_next_status_gpt,
.test_structure=test_structure_gpt,
.set_part_type=NULL,
-// .set_part_type=set_part_type_none,
.is_part_known=is_part_known_gpt,
.init_structure=init_structure_gpt,
.erase_list_part=NULL,
.get_partition_typename=get_partition_typename_gpt,
-// .get_part_type=get_part_type_gpt
-// .get_part_type=get_part_type_none
.get_part_type=NULL
};
@@ -186,17 +174,20 @@ list_part_t *read_part_gpt(disk_t *disk_car, const int verbose, const int savehe
free(gpt);
return NULL;
}
- log_info("hdr_size=%llu\n", (long long unsigned)le32(gpt->hdr_size));
- log_info("hdr_lba_self=%llu\n", (long long unsigned)le64(gpt->hdr_lba_self));
- log_info("hdr_lba_alt=%llu (expected %llu)\n",
- (long long unsigned)le64(gpt->hdr_lba_alt),
- (long long unsigned)((disk_car->disk_size-1)/disk_car->sector_size));
- log_info("hdr_lba_start=%llu\n", (long long unsigned)le64(gpt->hdr_lba_start));
- log_info("hdr_lba_end=%llu\n", (long long unsigned)le64(gpt->hdr_lba_end));
- log_info("hdr_lba_table=%llu\n",
- (long long unsigned)le64(gpt->hdr_lba_table));
- log_info("hdr_entries=%llu\n", (long long unsigned)le32(gpt->hdr_entries));
- log_info("hdr_entsz=%llu\n", (long long unsigned)le32(gpt->hdr_entsz));
+ if(verbose>0)
+ {
+ log_info("hdr_size=%llu\n", (long long unsigned)le32(gpt->hdr_size));
+ log_info("hdr_lba_self=%llu\n", (long long unsigned)le64(gpt->hdr_lba_self));
+ log_info("hdr_lba_alt=%llu (expected %llu)\n",
+ (long long unsigned)le64(gpt->hdr_lba_alt),
+ (long long unsigned)((disk_car->disk_size-1)/disk_car->sector_size));
+ log_info("hdr_lba_start=%llu\n", (long long unsigned)le64(gpt->hdr_lba_start));
+ log_info("hdr_lba_end=%llu\n", (long long unsigned)le64(gpt->hdr_lba_end));
+ log_info("hdr_lba_table=%llu\n",
+ (long long unsigned)le64(gpt->hdr_lba_table));
+ log_info("hdr_entries=%llu\n", (long long unsigned)le32(gpt->hdr_entries));
+ log_info("hdr_entsz=%llu\n", (long long unsigned)le32(gpt->hdr_entsz));
+ }
/* Check header size */
if(le32(gpt->hdr_size)<92 || le32(gpt->hdr_size) > disk_car->sector_size)
{
@@ -715,23 +706,6 @@ static int test_structure_gpt(list_part_t *list_part)
return res;
}
-#if 0
-static efi_guid_t get_part_type_gpt(const partition_t *partition)
-{
- return partition->part_type_gpt;
-}
-
-static int set_part_type_gpt(partition_t *partition, efi_guid_t part_type_gpt)
-{
- if(guid_cmp(partition->part_type_gpt, GPT_ENT_TYPE_UNUSED)!=0)
- {
- guid_cpy(&partition->part_type_gpt, &part_type_gpt);
- return 0;
- }
- return 1;
-}
-#endif
-
static int is_part_known_gpt(const partition_t *partition)
{
return (guid_cmp(partition->part_type_gpt, GPT_ENT_TYPE_UNUSED)!=0);
@@ -806,6 +780,7 @@ static int check_part_gpt(disk_t *disk_car,const int verbose,partition_t *partit
if(ret!=0)
{
log_error("check_part_gpt failed for partition\n");
+ log_partition(disk_car, partition);
aff_part_buffer(AFF_PART_ORDER|AFF_PART_STATUS,disk_car,partition);
if(saveheader>0)
{
diff --git a/src/partgpt.h b/src/partgpt.h
index 476feca..9de52c9 100644
--- a/src/partgpt.h
+++ b/src/partgpt.h
@@ -1,3 +1,24 @@
+/*
+
+ File: partgpt.h
+
+ Copyright (C) 2007-2008 Christophe GRENIER <grenier@cgsecurity.org>
+
+ This software is free software; you can redistribute it and/or modify
+ it under the terms of the GNU General Public License as published by
+ the Free Software Foundation; either version 2 of the License, or
+ (at your option) any later version.
+
+ This program is distributed in the hope that it will be useful,
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ GNU General Public License for more details.
+
+ You should have received a copy of the GNU General Public License along
+ with this program; if not, write the Free Software Foundation, Inc., 51
+ Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
+
+ */
#ifndef _PARTGPT_H
#define _PARTGPT_H
@@ -33,4 +54,10 @@ struct gpt_ent
uint8_t ent_name[72]; /* UNICODE-16 */
};
+struct systypes_gtp {
+ const efi_guid_t part_type;
+ const char *name;
+};
+
+
#endif /* _PARTGPT_H */