feat(partition-table): add BOOTLOADER and PARTITION_TABLE types

Add ESP-IDF partition types 0x02 (BOOTLOADER) and 0x03 (PARTITION_TABLE)
with their subtype enums and name maps. Refactor getSubtypeName/subtypeFromName
to use a unified SUBTYPE_NAME_MAPS lookup table.
This commit is contained in:
kerms 2026-03-05 18:20:04 +01:00
parent 4959ff74c1
commit c8b560387b
Signed by: kerms
GPG Key ID: 5432C10DDCF8DAD5
2 changed files with 54 additions and 19 deletions

View File

@ -2,8 +2,8 @@
export type { PartitionEntry, PartitionTable } from './types';
export {
PartitionType, AppSubtype, DataSubtype, PartitionFlags,
TYPE_NAMES, APP_SUBTYPE_NAMES, DATA_SUBTYPE_NAMES,
PartitionType, AppSubtype, DataSubtype, BootloaderSubtype, PartTableSubtype, PartitionFlags,
TYPE_NAMES, APP_SUBTYPE_NAMES, DATA_SUBTYPE_NAMES, BOOTLOADER_SUBTYPE_NAMES, PART_TABLE_SUBTYPE_NAMES,
NAME_TO_TYPE,
getSubtypeName, subtypeFromName,
} from './types';

View File

@ -1,7 +1,9 @@
/** Partition type (top-level: app or data) */
/** Partition type */
export enum PartitionType {
APP = 0x00,
DATA = 0x01,
BOOTLOADER = 0x02,
PARTITION_TABLE = 0x03,
}
/** App subtypes */
@ -40,6 +42,19 @@ export enum DataSubtype {
LITTLEFS = 0x83,
}
/** Bootloader subtypes */
export enum BootloaderSubtype {
PRIMARY = 0x00,
OTA = 0x01,
RECOVERY = 0x02,
}
/** Partition table subtypes */
export enum PartTableSubtype {
PRIMARY = 0x00,
OTA = 0x01,
}
/** Partition flags */
export enum PartitionFlags {
NONE = 0x00,
@ -69,6 +84,8 @@ export interface PartitionTable {
export const TYPE_NAMES: Record<number, string> = {
[PartitionType.APP]: 'app',
[PartitionType.DATA]: 'data',
[PartitionType.BOOTLOADER]: 'bootloader',
[PartitionType.PARTITION_TABLE]: 'partition_table',
};
/** Human-readable app subtype name map */
@ -95,29 +112,47 @@ export const DATA_SUBTYPE_NAMES: Record<number, string> = {
[DataSubtype.LITTLEFS]: 'littlefs',
};
/** Human-readable bootloader subtype name map */
export const BOOTLOADER_SUBTYPE_NAMES: Record<number, string> = {
[BootloaderSubtype.PRIMARY]: 'primary',
[BootloaderSubtype.OTA]: 'ota',
[BootloaderSubtype.RECOVERY]: 'recovery',
};
/** Human-readable partition table subtype name map */
export const PART_TABLE_SUBTYPE_NAMES: Record<number, string> = {
[PartTableSubtype.PRIMARY]: 'primary',
[PartTableSubtype.OTA]: 'ota',
};
/** Subtype name maps keyed by PartitionType */
const SUBTYPE_NAME_MAPS: Partial<Record<PartitionType, Record<number, string>>> = {
[PartitionType.APP]: APP_SUBTYPE_NAMES,
[PartitionType.DATA]: DATA_SUBTYPE_NAMES,
[PartitionType.BOOTLOADER]: BOOTLOADER_SUBTYPE_NAMES,
[PartitionType.PARTITION_TABLE]: PART_TABLE_SUBTYPE_NAMES,
};
/** Get human-readable subtype name */
export function getSubtypeName(type: PartitionType, subtype: number): string {
if (type === PartitionType.APP) {
return APP_SUBTYPE_NAMES[subtype] ?? `0x${subtype.toString(16).padStart(2, '0')}`;
}
return DATA_SUBTYPE_NAMES[subtype] ?? `0x${subtype.toString(16).padStart(2, '0')}`;
const map = SUBTYPE_NAME_MAPS[type];
return map?.[subtype] ?? `0x${subtype.toString(16).padStart(2, '0')}`;
}
/** Reverse lookup: type name string → PartitionType */
export const NAME_TO_TYPE: Record<string, PartitionType> = {
'app': PartitionType.APP,
'data': PartitionType.DATA,
'bootloader': PartitionType.BOOTLOADER,
'partition_table': PartitionType.PARTITION_TABLE,
};
/** Reverse lookup: subtype name → number, keyed by parent type */
export function subtypeFromName(type: PartitionType, name: string): number {
const normalized = name.trim().toLowerCase();
if (type === PartitionType.APP) {
for (const [val, n] of Object.entries(APP_SUBTYPE_NAMES)) {
if (n === normalized) return Number(val);
}
} else {
for (const [val, n] of Object.entries(DATA_SUBTYPE_NAMES)) {
const map = SUBTYPE_NAME_MAPS[type];
if (map) {
for (const [val, n] of Object.entries(map)) {
if (n === normalized) return Number(val);
}
}