nyx: tools: add mbr hidden attr fixer in hybrid mbr fixer

This commit is contained in:
CTCaer 2022-05-08 05:59:32 +03:00
parent 471f3c50ea
commit c51877d588

View file

@ -2058,6 +2058,7 @@ static lv_res_t _action_fix_mbr(lv_obj_t *btn)
mbr_t mbr[2] = { 0 }; mbr_t mbr[2] = { 0 };
gpt_t *gpt = calloc(1, sizeof(gpt_t)); gpt_t *gpt = calloc(1, sizeof(gpt_t));
gpt_header_t gpt_hdr_backup = { 0 };
sdmmc_storage_read(&sd_storage, 0, 1, &mbr[0]); sdmmc_storage_read(&sd_storage, 0, 1, &mbr[0]);
sdmmc_storage_read(&sd_storage, 1, sizeof(gpt_t) >> 9, gpt); sdmmc_storage_read(&sd_storage, 1, sizeof(gpt_t) >> 9, gpt);
@ -2069,9 +2070,12 @@ static lv_res_t _action_fix_mbr(lv_obj_t *btn)
if (memcmp(&gpt->header.signature, "EFI PART", 8) || gpt->header.num_part_ents > 128) if (memcmp(&gpt->header.signature, "EFI PART", 8) || gpt->header.num_part_ents > 128)
{ {
lv_label_set_text(lbl_status, "#FFDD00 Warning:# No valid GPT was found!"); lv_label_set_text(lbl_status, "#FFDD00 Warning:# No valid GPT was found!");
free(gpt);
goto out; goto out;
} }
sdmmc_storage_read(&sd_storage, gpt->header.alt_lba, 1, &gpt_hdr_backup);
// Parse GPT. // Parse GPT.
LIST_INIT(gpt_parsed); LIST_INIT(gpt_parsed);
for (u32 i = 0; i < gpt->header.num_part_ents; i++) for (u32 i = 0; i < gpt->header.num_part_ents; i++)
@ -2092,7 +2096,6 @@ static lv_res_t _action_fix_mbr(lv_obj_t *btn)
list_append(&gpt_parsed, &part->link); list_append(&gpt_parsed, &part->link);
} }
free(gpt);
// Set FAT and emuMMC partitions. // Set FAT and emuMMC partitions.
u32 mbr_idx = 1; u32 mbr_idx = 1;
@ -2138,21 +2141,27 @@ static lv_res_t _action_fix_mbr(lv_obj_t *btn)
mbr[1].partitions[mbr_idx].size_sct = sd_storage.sec_cnt - 1; mbr[1].partitions[mbr_idx].size_sct = sd_storage.sec_cnt - 1;
// Check for differences. // Check for differences.
bool changed = false; bool hybrid_mbr_changed = false;
for (u32 i = 1; i < 4; i++) for (u32 i = 1; i < 4; i++)
{ {
if ((mbr[0].partitions[i].type != mbr[1].partitions[i].type) || if ((mbr[0].partitions[i].type != mbr[1].partitions[i].type) ||
(mbr[0].partitions[i].start_sct != mbr[1].partitions[i].start_sct) || (mbr[0].partitions[i].start_sct != mbr[1].partitions[i].start_sct) ||
(mbr[0].partitions[i].size_sct != mbr[1].partitions[i].size_sct)) (mbr[0].partitions[i].size_sct != mbr[1].partitions[i].size_sct))
{ {
changed = true; hybrid_mbr_changed = true;
break; break;
} }
} }
if (!changed) // Check for secret MBR attributes.
bool has_mbr_attributes = false;
if (gpt->entries[0].part_guid[7])
has_mbr_attributes = true;
if (!hybrid_mbr_changed && !has_mbr_attributes)
{ {
lv_label_set_text(lbl_status, "#96FF00 Warning:# The Hybrid MBR needs no change!#"); lv_label_set_text(lbl_status, "#96FF00 Warning:# The Hybrid MBR needs no change!#");
free(gpt);
goto out; goto out;
} }
@ -2200,14 +2209,44 @@ static lv_res_t _action_fix_mbr(lv_obj_t *btn)
if (btn_wait() & BTN_POWER) if (btn_wait() & BTN_POWER)
{ {
// Write MBR.
sd_mount(); sd_mount();
// Write MBR.
if (hybrid_mbr_changed)
sdmmc_storage_write(&sd_storage, 0, 1, &mbr[1]); sdmmc_storage_write(&sd_storage, 0, 1, &mbr[1]);
// Fix MBR secret attributes.
if (has_mbr_attributes)
{
gpt->entries[0].part_guid[7] = 0;
u32 entries_size = sizeof(gpt_entry_t) * gpt->header.num_part_ents;
gpt->header.part_ents_crc32 = crc32_calc(0, (const u8 *)gpt->entries, entries_size);
gpt->header.crc32 = 0; // Set to 0 for calculation.
gpt->header.crc32 = crc32_calc(0, (const u8 *)&gpt->header, gpt->header.size);
gpt_hdr_backup.part_ents_crc32 = gpt->header.part_ents_crc32;
gpt_hdr_backup.crc32 = 0; // Set to 0 for calculation.
gpt_hdr_backup.crc32 = crc32_calc(0, (const u8 *)&gpt_hdr_backup, gpt_hdr_backup.size);
// Write main GPT.
u32 aligned_entries_size = ALIGN(entries_size, 512);
sdmmc_storage_write(&sd_storage, gpt->header.my_lba, (sizeof(gpt_header_t) + aligned_entries_size) >> 9, gpt);
// Write backup GPT partition table.
sdmmc_storage_write(&sd_storage, gpt_hdr_backup.part_ent_lba, aligned_entries_size >> 9, gpt->entries);
// Write backup GPT header.
sdmmc_storage_write(&sd_storage, gpt_hdr_backup.my_lba, 1, &gpt_hdr_backup);
}
sd_unmount(); sd_unmount();
lv_label_set_text(lbl_status, "#96FF00 The new Hybrid MBR was written successfully!#"); lv_label_set_text(lbl_status, "#96FF00 The new Hybrid MBR was written successfully!#");
} }
else else
lv_label_set_text(lbl_status, "#FFDD00 Warning: The Hybrid MBR Fix was canceled!#"); lv_label_set_text(lbl_status, "#FFDD00 Warning: The Hybrid MBR Fix was canceled!#");
free(gpt);
out: out:
lv_mbox_add_btns(mbox, mbox_btn_map, mbox_action); lv_mbox_add_btns(mbox, mbox_btn_map, mbox_action);