Add support for displaying memory usage.

Signed-off-by: Pol Henarejos <pol.henarejos@cttc.es>
This commit is contained in:
Pol Henarejos 2024-12-23 20:24:10 +01:00
parent 74b635fa3c
commit d56b540324
No known key found for this signature in database
GPG key ID: C0095B7870A4CCD3
3 changed files with 48 additions and 9 deletions

@ -1 +1 @@
Subproject commit 6859cedcbf7a98227bd530a40152d9fde7332a73
Subproject commit ffaf20da5d65a2dfc6c92026014f818ec9382f21

View file

@ -43,11 +43,13 @@
#define SECURE_LOCK_DISABLE 0x4
#define CMD_PHY 0x1B
#define CMD_OTP 0x4C
#define CMD_MEMORY 0x5
int cmd_extras() {
int cmd = P1(apdu);
#ifndef ENABLE_EMULATION
// Only allow change PHY without PIN
if (!isUserAuthenticated && P1(apdu) != 0x1B) {
if (!isUserAuthenticated && cmd != CMD_PHY && cmd != CMD_MEMORY) {
return SW_SECURITY_STATUS_NOT_SATISFIED();
}
#endif
@ -55,7 +57,7 @@ int cmd_extras() {
if (wait_button_pressed() == true) {
return SW_SECURE_MESSAGE_EXEC_ERROR();
}
if (P1(apdu) == CMD_DATETIME) { //datetime operations
if (cmd == CMD_DATETIME) { //datetime operations
if (P2(apdu) != 0x0) {
return SW_INCORRECT_P1P2();
}
@ -99,7 +101,7 @@ int cmd_extras() {
#endif
}
}
else if (P1(apdu) == CMD_DYNOPS) { //dynamic options
else if (cmd == CMD_DYNOPS) { //dynamic options
if (P2(apdu) != 0x0) {
return SW_INCORRECT_P1P2();
}
@ -118,7 +120,7 @@ int cmd_extras() {
low_flash_available();
}
}
else if (P1(apdu) == CMD_SECURE_LOCK) { // secure lock
else if (cmd == CMD_SECURE_LOCK) { // secure lock
if (apdu.nc == 0) {
return SW_WRONG_LENGTH();
}
@ -202,7 +204,7 @@ int cmd_extras() {
}
}
#ifndef ENABLE_EMULATION
else if (P1(apdu) == CMD_PHY) { // Set PHY
else if (cmd == CMD_PHY) { // Set PHY
if (apdu.nc == 0) {
if (file_has_data(ef_phy)) {
res_APDU_size = file_get_size(ef_phy);
@ -247,7 +249,7 @@ int cmd_extras() {
}
#endif
#if PICO_RP2350
else if (P1(apdu) == CMD_OTP) {
else if (cmd == CMD_OTP) {
if (apdu.nc < 2) {
return SW_WRONG_LENGTH();
}
@ -290,13 +292,37 @@ int cmd_extras() {
}
#endif
#ifdef PICO_PLATFORM
else if (P1(apdu) == CMD_REBOOT) {
else if (cmd == CMD_REBOOT) {
if (apdu.nc != 0) {
return SW_WRONG_LENGTH();
}
watchdog_reboot(0, 0, 100);
}
#endif
else if (cmd == CMD_MEMORY) {
res_APDU_size = 0;
uint32_t free = flash_free_space(), total = flash_total_space(), used = flash_used_space(), nfiles = flash_num_files(), size = flash_size();
res_APDU[res_APDU_size++] = free >> 24;
res_APDU[res_APDU_size++] = free >> 16;
res_APDU[res_APDU_size++] = free >> 8;
res_APDU[res_APDU_size++] = free;
res_APDU[res_APDU_size++] = used >> 24;
res_APDU[res_APDU_size++] = used >> 16;
res_APDU[res_APDU_size++] = used >> 8;
res_APDU[res_APDU_size++] = used;
res_APDU[res_APDU_size++] = total >> 24;
res_APDU[res_APDU_size++] = total >> 16;
res_APDU[res_APDU_size++] = total >> 8;
res_APDU[res_APDU_size++] = total;
res_APDU[res_APDU_size++] = nfiles >> 24;
res_APDU[res_APDU_size++] = nfiles >> 16;
res_APDU[res_APDU_size++] = nfiles >> 8;
res_APDU[res_APDU_size++] = nfiles;
res_APDU[res_APDU_size++] = size >> 24;
res_APDU[res_APDU_size++] = size >> 16;
res_APDU[res_APDU_size++] = size >> 8;
res_APDU[res_APDU_size++] = size;
}
else {
return SW_INCORRECT_P1P2();
}

View file

@ -151,6 +151,8 @@ def parse_args():
parser_reboot = subparser.add_parser('reboot', help='Reboots the Pico HSM.')
parser_memory = subparser.add_parser('memory', help='Get memory usage.')
args = parser.parse_args()
return args
@ -512,8 +514,17 @@ def otp(picohsm, args):
def reboot(picohsm, args):
picohsm.reboot()
def memory(picohsm, args):
mem = picohsm.memory()
print(f'Memory usage:')
print(f'\tFree: {mem["free"]/1024:.2f} kilobytes ({mem["free"]*100/mem["total"]:.2f}%)')
print(f'\tUsed: {mem["used"]/1024:.2f} kilobytes ({mem["used"]*100/mem["total"]:.2f}%)')
print(f'\tTotal: {mem["total"]/1024:.2f} kilobytes')
print(f'\tFlash size: {mem["size"]/1024:.2f} kilobytes')
print(f'\tFiles: {mem["files"]}')
def main(args):
sys.stderr.buffer.write(b'Pico HSM Tool v2.0\n')
sys.stderr.buffer.write(b'Pico HSM Tool v2.2\n')
sys.stderr.buffer.write(b'Author: Pol Henarejos\n')
sys.stderr.buffer.write(b'Report bugs to https://github.com/polhenarejos/pico-hsm/issues\n')
sys.stderr.buffer.write(b'\n\n')
@ -544,6 +555,8 @@ def main(args):
otp(picohsm, args)
elif (args.command == 'reboot'):
reboot(picohsm, args)
elif (args.command == 'memory'):
memory(picohsm, args)
def run():
args = parse_args()