mirror of
https://github.com/tillitis/tillitis-key1.git
synced 2025-04-27 10:29:22 -04:00
45 lines
1.0 KiB
C
45 lines
1.0 KiB
C
// Copyright (C) 2024 - Tillitis AB
|
|
// SPDX-License-Identifier: GPL-2.0-only
|
|
|
|
#include <stdint.h>
|
|
#include <tkey/lib.h>
|
|
|
|
#include "flash.h"
|
|
#include "partition_table.h"
|
|
#include "proto.h"
|
|
|
|
int part_table_read(struct partition_table *part_table)
|
|
{
|
|
// Read from flash, if it exists, otherwise create a new one.
|
|
|
|
flash_release_powerdown();
|
|
memset(part_table, 0x00, sizeof(*part_table));
|
|
|
|
flash_read_data(ADDR_PARTITION_TABLE, (uint8_t *)part_table,
|
|
sizeof(*part_table));
|
|
|
|
// TODO: Implement redundancy and consistency check
|
|
|
|
if (part_table->header.version != PART_TABLE_VERSION) {
|
|
// Partition table is not ours. Make a new one, and store it.
|
|
memset(part_table, 0x00, sizeof(*part_table));
|
|
|
|
part_table->header.version = PART_TABLE_VERSION;
|
|
|
|
part_table_write(part_table);
|
|
}
|
|
|
|
// Now the partition table is synced between flash and RAM.
|
|
|
|
return 0;
|
|
}
|
|
|
|
int part_table_write(struct partition_table *part_table)
|
|
{
|
|
flash_sector_erase(ADDR_PARTITION_TABLE);
|
|
flash_write_data(ADDR_PARTITION_TABLE, (uint8_t *)part_table,
|
|
sizeof(*part_table));
|
|
|
|
return 0;
|
|
}
|