Skip to content

Commit

Permalink
wip: cmds: introduce ptable command
Browse files Browse the repository at this point in the history
  • Loading branch information
gerard5 committed Jun 25, 2024
1 parent ed09ef9 commit 30de5c4
Show file tree
Hide file tree
Showing 11 changed files with 464 additions and 5 deletions.
2 changes: 1 addition & 1 deletion cmds/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

PLO_ALLCOMMANDS = alias app bankswitch bitstream blob bootcm4 bootrom bridge call console \
copy devices dump echo erase go help jffs2 kernel kernelimg lspci map mem mpu otp phfs \
reboot script stop test-dev test-ddr wait
ptable reboot script stop test-dev test-ddr wait

PLO_COMMANDS ?= $(PLO_ALLCOMMANDS)
PLO_APPLETS = $(filter $(PLO_ALLCOMMANDS), $(PLO_COMMANDS))
Expand Down
152 changes: 152 additions & 0 deletions cmds/ptable.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,152 @@
/*
* Phoenix-RTOS
*
* Operating system loader
*
* partition table tool
*
* Copyright 2024 Phoenix Systems
* Author: Gerard Swiderski
*
* This file is part of Phoenix-RTOS.
*
* %LICENSE%
*/

#include "cmd.h"

#include <devices/devs.h>
#include <hal/hal.h>
#include <lib/lib.h>

#include <phfs/phfs.h>

#define CSI_RESET "\033[0m"
#define CSI_BOLD "\033[1m"

#define PTABLE_HEADER_FORMAT "%2s %-10s %10s %10s %10s %10s %-8s\n"
#define PTABLE_ENTRY_FORMAT "%2u %-10s %10u %10u %10u %10u %-12s\n"


static struct {
u32 memsz;
u32 blksz;
ptable_t ptable[1024];
} ptable_common;


static void cmd_ptableInfo(void)
{
lib_printf("print partition table, usage: ptable <dev> [<offset>]");
}


static int partPrint(ptable_t *p)
{
unsigned int i = p->count;
unsigned int j = 0;

lib_printf(
"\n" CSI_BOLD PTABLE_HEADER_FORMAT CSI_RESET,
"#", "Name", "Start", "End", "Blocks", "Size", "Type");

while (i-- != 0) {
ptable_part_t *entry = &p->parts[i];
lib_printf(
PTABLE_ENTRY_FORMAT,
++j, entry->name, entry->offset, entry->offset + entry->size,
entry->size / ptable_common.blksz, entry->size,
ptable_typeName(entry->type));
}

return 0;
}


static int partRead(handler_t h, addr_t offs)
{
u32 partCount;
size_t ptableSize;

int res = phfs_read(h, offs, &partCount, sizeof(partCount));
if (res < 0) {
log_error("\nCan't read data");
return res;
}

if (partCount == 0) {
log_error("\nNo partitions at offset 0x%x", offs);
return 0;
}

ptableSize = ptable_size(partCount);
if (ptableSize > sizeof(ptable_common.ptable)) {
log_error("\nIncorrect partition table at offset 0x%x", offs);
return -1;
}

res = phfs_read(h, offs, ptable_common.ptable, ptableSize);
if (res < 0) {
log_error("\nCan't read data");
return res;
}

res = ptable_deserialize(ptable_common.ptable, ptable_common.memsz, ptable_common.blksz);
if (res < 0) {
log_error("\nIncorrect partition table at offset 0x%x", offs);
return res;
}


return (int)partCount;
}


static int cmd_ptable(int argc, char *argv[])
{
int res;
addr_t offs = 0;
char *endptr = NULL;
handler_t h;

/* FIXME: add info to phfs */
ptable_common.memsz = 32 * 1024 * 1024;
ptable_common.blksz = 4096;

if ((argc != 2) && (argc != 3)) {
log_error("\n%s: Wrong argument count", argv[0]);
return -EINVAL;
}

if (argc > 2) {
offs = lib_strtoul(argv[2], &endptr, 0);
if (argv[2] == endptr) {
log_error("\n%s: Wrong arguments", argv[0]);
return -EINVAL;
}
}

if (phfs_open(argv[1], NULL, PHFS_OPEN_RAWONLY, &h) < 0) {
lib_printf("\n%s: Invalid phfs name provided: %s\n", argv[0], argv[1]);
return CMD_EXIT_FAILURE;
}

res = partRead(h, offs);

(void)phfs_close(h);

if (res <= 0) {
return CMD_EXIT_FAILURE;
}

if (res > 0) {
partPrint(ptable_common.ptable);
}

return CMD_EXIT_SUCCESS;
}


static const cmd_t ptable_cmd __attribute__((section("commands"), used)) = {
.name = "ptable", .run = cmd_ptable, .info = cmd_ptableInfo
};
2 changes: 1 addition & 1 deletion hal/armv7m/imxrt/10xx/105x/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ CFLAGS:=$(filter-out -mfloat-abi% , $(CFLAGS))
CFLAGS+= -mfloat-abi=soft

PLO_COMMANDS ?= alias app blob bridge call console copy devices dump echo erase go help kernel \
kernelimg map mem mpu otp phfs reboot script stop wait
kernelimg map mem mpu otp phfs ptable reboot script stop wait

# pipe-rtt is disabled due to small amount of space in DTCM; RTT_ADDR is not defined for this architecture
PLO_ALLDEVICES := usbc-cdc uart-imxrt106x flash-imxrt
Expand Down
2 changes: 1 addition & 1 deletion hal/armv7m/imxrt/10xx/106x/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ CFLAGS:=$(filter-out -mfloat-abi% , $(CFLAGS))
CFLAGS+= -mfloat-abi=soft

PLO_COMMANDS ?= alias app blob bootrom bridge call console copy devices dump echo erase go \
help kernel kernelimg map mem mpu otp phfs reboot script stop wait
help kernel kernelimg map mem mpu otp phfs ptable reboot script stop wait

PLO_ALLDEVICES := pipe-rtt usbc-cdc uart-imxrt106x flash-imxrt

Expand Down
2 changes: 1 addition & 1 deletion hal/armv7m/imxrt/117x/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ CFLAGS := $(filter-out -mfloat-abi% , $(CFLAGS))
CFLAGS += -mfloat-abi=soft

PLO_COMMANDS ?= alias app blob bootcm4 bridge bootrom call console copy devices dump echo erase go \
help kernel kernelimg map mem mpu otp phfs reboot script stop wait
help kernel kernelimg map mem mpu otp phfs ptable reboot script stop wait

PLO_ALLDEVICES := pipe-rtt usbc-cdc uart-imxrt117x flash-imxrt

Expand Down
2 changes: 1 addition & 1 deletion lib/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,4 @@
# %LICENSE%
#

OBJS += $(addprefix $(PREFIX_O)lib/, console.o ctype.o crc32.o cbuffer.o format.o getopt.o list.o log.o printf.o prompt.o sprintf.o strtoul.o)
OBJS += $(addprefix $(PREFIX_O)lib/, console.o ctype.o crc32.o cbuffer.o format.o getopt.o list.o log.o printf.o prompt.o ptable.o sprintf.o strtoul.o)
6 changes: 6 additions & 0 deletions lib/ctype.c
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,12 @@ int lib_isdigit(int c)
}


int lib_isalnum(int c)
{
return lib_islower(c) || lib_isupper(c) || lib_isdigit(c);
}


int lib_isblank(int c)
{
return (c == ' ') || (c == '\t');
Expand Down
3 changes: 3 additions & 0 deletions lib/ctype.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,9 @@ extern int lib_isupper(int c);
extern int lib_isalpha(int c);


extern int lib_isalnum(int c);


extern int lib_isdigit(int c);


Expand Down
3 changes: 3 additions & 0 deletions lib/lib.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
#include "stdarg.h"
#include "prompt.h"
#include "crc32.h"
#include "ptable.h"


#define min(a, b) ({ \
Expand All @@ -41,6 +42,8 @@
_a > _b ? _a : _b; \
})

#define offsetof(a, b) ((size_t)(&(((a *)(0))->b)))


extern int lib_printf(const char *fmt, ...);

Expand Down
Loading

0 comments on commit 30de5c4

Please sign in to comment.