2019-03-05 00:05:42 +01:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2018 naehrwert
|
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or modify it
|
|
|
|
* under the terms and conditions of the GNU General Public License,
|
|
|
|
* version 2, as published by the Free Software Foundation.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope 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, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <string.h>
|
|
|
|
|
2020-05-18 22:11:27 +02:00
|
|
|
#include "mbr_gpt.h"
|
2019-03-05 00:05:42 +01:00
|
|
|
#include "nx_emmc.h"
|
2019-09-17 17:51:30 +02:00
|
|
|
#include "emummc.h"
|
2019-03-05 00:05:42 +01:00
|
|
|
#include "../mem/heap.h"
|
|
|
|
#include "../utils/list.h"
|
|
|
|
|
|
|
|
void nx_emmc_gpt_parse(link_t *gpt, sdmmc_storage_t *storage)
|
|
|
|
{
|
2020-05-18 22:11:27 +02:00
|
|
|
gpt_t *gpt_buf = (gpt_t *)calloc(NX_GPT_NUM_BLOCKS, NX_EMMC_BLOCKSIZE);
|
2019-03-05 00:05:42 +01:00
|
|
|
|
2020-05-18 22:11:27 +02:00
|
|
|
emummc_storage_read(storage, NX_GPT_FIRST_LBA, NX_GPT_NUM_BLOCKS, gpt_buf);
|
2019-03-05 00:05:42 +01:00
|
|
|
|
2020-05-18 22:11:27 +02:00
|
|
|
for (u32 i = 0; i < gpt_buf->header.num_part_ents; i++)
|
2019-03-05 00:05:42 +01:00
|
|
|
{
|
2020-04-16 00:18:58 +02:00
|
|
|
emmc_part_t *part = (emmc_part_t *)calloc(sizeof(emmc_part_t), 1);
|
2020-05-18 22:11:27 +02:00
|
|
|
|
|
|
|
if (gpt_buf->entries[i].lba_start < gpt_buf->header.first_use_lba)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
part->index = i;
|
|
|
|
part->lba_start = gpt_buf->entries[i].lba_start;
|
|
|
|
part->lba_end = gpt_buf->entries[i].lba_end;
|
|
|
|
part->attrs = gpt_buf->entries[i].attrs;
|
2019-03-05 00:05:42 +01:00
|
|
|
|
2019-12-09 03:17:46 +01:00
|
|
|
// ASCII conversion. Copy only the LSByte of the UTF-16LE name.
|
2020-05-18 22:11:27 +02:00
|
|
|
for (u32 j = 0; j < 36; j++)
|
|
|
|
part->name[j] = gpt_buf->entries[i].name[j];
|
|
|
|
part->name[35] = 0;
|
2019-03-05 00:05:42 +01:00
|
|
|
|
|
|
|
list_append(gpt, &part->link);
|
|
|
|
}
|
|
|
|
|
2020-05-18 22:11:27 +02:00
|
|
|
free(gpt_buf);
|
2019-03-05 00:05:42 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void nx_emmc_gpt_free(link_t *gpt)
|
|
|
|
{
|
|
|
|
LIST_FOREACH_SAFE(iter, gpt)
|
|
|
|
free(CONTAINER_OF(iter, emmc_part_t, link));
|
|
|
|
}
|
|
|
|
|
|
|
|
emmc_part_t *nx_emmc_part_find(link_t *gpt, const char *name)
|
|
|
|
{
|
|
|
|
LIST_FOREACH_ENTRY(emmc_part_t, part, gpt, link)
|
|
|
|
if (!strcmp(part->name, name))
|
|
|
|
return part;
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
int nx_emmc_part_read(sdmmc_storage_t *storage, emmc_part_t *part, u32 sector_off, u32 num_sectors, void *buf)
|
|
|
|
{
|
|
|
|
// The last LBA is inclusive.
|
|
|
|
if (part->lba_start + sector_off > part->lba_end)
|
|
|
|
return 0;
|
2019-09-17 17:51:30 +02:00
|
|
|
return emummc_storage_read(storage, part->lba_start + sector_off, num_sectors, buf);
|
2019-03-05 00:05:42 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
int nx_emmc_part_write(sdmmc_storage_t *storage, emmc_part_t *part, u32 sector_off, u32 num_sectors, void *buf)
|
|
|
|
{
|
|
|
|
// The last LBA is inclusive.
|
|
|
|
if (part->lba_start + sector_off > part->lba_end)
|
|
|
|
return 0;
|
|
|
|
return sdmmc_storage_write(storage, part->lba_start + sector_off, num_sectors, buf);
|
|
|
|
}
|