Skip to content

Commit

Permalink
Add support for displaying memory usage.
Browse files Browse the repository at this point in the history
Signed-off-by: Pol Henarejos <[email protected]>
  • Loading branch information
polhenarejos committed Dec 23, 2024
1 parent 74b635f commit d56b540
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 9 deletions.
2 changes: 1 addition & 1 deletion pico-keys-sdk
40 changes: 33 additions & 7 deletions src/hsm/cmd_extras.c
Original file line number Diff line number Diff line change
Expand Up @@ -43,19 +43,21 @@
#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
//check button (if enabled)
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();
}
Expand Down Expand Up @@ -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();
}
Expand All @@ -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();
}
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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();
}
Expand Down Expand Up @@ -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();
}
Expand Down
15 changes: 14 additions & 1 deletion tools/pico-hsm-tool.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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')
Expand Down Expand Up @@ -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()
Expand Down

0 comments on commit d56b540

Please sign in to comment.