-
Notifications
You must be signed in to change notification settings - Fork 5
Open
Description
Current API for writing to app storage is not very kind to the developer. Offset must always be multiple of 256.
During work on tkey-boot-verifier we added the following to cope with the alignment when storing a preloaded app (which has the same properties as other syscalls). I propose we use something similar in tkey-libs to hide the complexity in writing.
int write_app(uint32_t addr, uint8_t *data, size_t sz)
{
uint8_t buf[WRITE_SIZE];
uint32_t buf_offset = addr & ~WRITE_ALIGN_MASK;
size_t len = 0;
debug_puts("app write addr=");
debug_putinthex(addr);
debug_puts(" size=");
debug_putinthex(sz);
for (size_t i = 0; i < sz; i += len) {
size_t bytes_left = sz - i;
size_t dst_max_len = WRITE_SIZE - buf_offset;
size_t src_max_len =
bytes_left < WRITE_SIZE ? bytes_left : WRITE_SIZE;
len = src_max_len < dst_max_len ? src_max_len : dst_max_len;
memset(buf, 0xff, sizeof(buf));
memcpy(buf + buf_offset, data + i, len);
int ret =
sys_preload_store(addr & WRITE_ALIGN_MASK, buf, WRITE_SIZE);
if (ret != 0) {
debug_puts("write app failed, addr=");
debug_putinthex(addr);
debug_puts(" ret=");
debug_putinthex(ret);
return -1;
}
buf_offset = 0;
addr += len;
}
return 0;
}Reactions are currently unavailable
Metadata
Metadata
Assignees
Labels
No labels
Type
Projects
Status
No status