sdk-hwV1.3/lichee/brandy-2.0/spl/common/part_efi.c

118 lines
2.5 KiB
C

#include <common.h>
#include <arch/spinor.h>
#include <part_efi.h>
static void *gpt_data_point;
#if GPT_DEBUG
static int gpt_show_partition_info(char *buf)
{
int i, j;
char char8_name[PARTNAME_SZ] = {0};
gpt_header *gpt_head = (gpt_header *)(buf + GPT_HEAD_OFFSET);
gpt_entry *entry = (gpt_entry *)(buf + GPT_ENTRY_OFFSET);
printf("---------------------------\n");
for (i = 0; i < gpt_head->num_partition_entries; i++) {
for (j = 0; j < PARTNAME_SZ; j++) {
char8_name[j] = (char)(entry[i].partition_name[j]);
}
printf("GPT:%s: 0x%lx 0x%lx\n", char8_name, (long)entry[i].starting_lba, (long)entry[i].ending_lba);
}
printf("---------------------------\n");
return 0;
}
#endif
static int is_gpt_valid(void *buffer)
{
gpt_header *gpt_head = (gpt_header *)(buffer + GPT_HEAD_OFFSET);
if (gpt_head->signature != GPT_HEADER_SIGNATURE) {
printf("gpt magic error, %llx != %llx\n", gpt_head->signature, GPT_HEADER_SIGNATURE);
return 0;
}
#if GPT_DEBUG
gpt_show_partition_info(buffer);
#endif
return 1;
}
int get_part_info_by_name(char *name, uint *start_sector, uint *sectors)
{
if (!gpt_data_point) {
printf("Still not initialized gpt,please initialize A first\n");
return -1;
}
int i, j;
char char8_name[PARTNAME_SZ] = {0};
gpt_header *gpt_head = (gpt_header *)(gpt_data_point + GPT_HEAD_OFFSET);
gpt_entry *entry = (gpt_entry *)(gpt_data_point + GPT_ENTRY_OFFSET);
if (gpt_head->signature != GPT_HEADER_SIGNATURE) {
printf("gpt magic error, %llx != %llx\n", gpt_head->signature, GPT_HEADER_SIGNATURE);
return -1;
}
for (i = 0; i < gpt_head->num_partition_entries; i++) {
for (j = 0; j < PARTNAME_SZ; j++) {
char8_name[j] = (char)(entry[i].partition_name[j]);
}
if (!strcmp(name, char8_name)) {
*start_sector = (uint)entry[i].starting_lba;
*sectors = ((uint)entry[i].ending_lba + 1) - (uint)entry[i].starting_lba;
return 0;
}
}
/* can't find the part */
printf("can't find the part %s\n", name);
return -1;
}
int init_gpt(void)
{
void *buffer = NULL;
buffer = malloc(SUNXI_MBR_SIZE);
if (!buffer) {
printf("GPT: malloc faile\n");
return -1;
}
memset(buffer, 0, SUNXI_MBR_SIZE);
if (load_gpt(buffer)) {
printf("load gpt fail\n");
goto error;
}
if (is_gpt_valid(buffer)) {
gpt_data_point = buffer;
} else {
printf("gpt data not valid\n");
ndump(buffer, 1024);
goto error;
}
return 0;
error:
free(buffer);
return -1;
}
int deinit_gpt(void)
{
if (gpt_data_point) {
memset(gpt_data_point, 0, SUNXI_MBR_SIZE);
free(gpt_data_point);
gpt_data_point = NULL;
}
return 0;
}