Skip to content

Commit

Permalink
alias: Add option to use relative alias offset
Browse files Browse the repository at this point in the history
DONE: RTOS-713
  • Loading branch information
agkaminski committed Dec 12, 2023
1 parent ff2329b commit 9e07f5e
Showing 1 changed file with 71 additions and 19 deletions.
90 changes: 71 additions & 19 deletions cmds/alias.c
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@
*
* Create file alias
*
* Copyright 2021 Phoenix Systems
* Author: Hubert Buczynski
* Copyright 2021, 2023 Phoenix Systems
* Author: Hubert Buczynski, Aleksander Kaminski
*
* This file is part of Phoenix-RTOS.
*
Expand All @@ -17,11 +17,16 @@

#include <hal/hal.h>
#include <phfs/phfs.h>
#include <lib/getopt.h>
#include <lib/log.h>


static addr_t aliasBase = 0;


static void cmd_aliasInfo(void)
{
lib_printf("sets alias to file, usage: alias [<name> <offset> <size>]");
lib_printf("sets alias to file, usage: alias [-b <base> | [-r] <name> <offset> <size>]");
}


Expand All @@ -30,38 +35,85 @@ static int cmd_alias(int argc, char *argv[])
char *end;
size_t sz = 0;
addr_t addr = 0;
unsigned int i;
int c, relative = 0;
addr_t newBase;

if (argc == 1) {
lib_printf("\nRelative alias base: 0x%p", aliasBase);
phfs_aliasesShow();
return EOK;
return CMD_EXIT_SUCCESS;
}
else if (argc != 4) {
log_error("\n%s: Wrong argument count", argv[0]);
return -EINVAL;

for (;;) {
c = lib_getopt(argc, argv, "rb:");
if (c < 0) {
break;
}

switch (c) {
case 'r':
relative = 1;
break;

case 'b':
if (argc != 3) {
log_error("\n%s: Invalid argument count.\n", argv[0]);
cmd_aliasInfo();
return CMD_EXIT_FAILURE;
}

newBase = lib_strtoul(optarg, &end, 0);
if (*end != '\0') {
log_error("\n%s: Invalid base.\n", argv[0]);
cmd_aliasInfo();
return CMD_EXIT_FAILURE;
}

aliasBase = newBase;

/* Ignore everything else, this is a separate function */
return CMD_EXIT_SUCCESS;

default:
log_error("\n%s: Invalid option: %c\n", argv[0], c);
cmd_aliasInfo();
return CMD_EXIT_FAILURE;
}
}

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

for (i = 2; i < 4; ++i) {
if (i == 2)
addr = lib_strtoul(argv[i], &end, 0);
else if (i == 3)
sz = lib_strtoul(argv[i], &end, 0);
addr = lib_strtoul(argv[optind + 1], &end, 0);
if (*end != '\0') {
log_error("\n%s: Wrong arguments", argv[0]);
return CMD_EXIT_FAILURE;
}

sz = lib_strtoul(argv[optind + 2], &end, 0);
if (*end != '\0') {
log_error("\n%s: Wrong arguments", argv[0]);
return CMD_EXIT_FAILURE;
}

if (*end) {
log_error("\n%s: Wrong arguments", argv[0]);
return -EINVAL;
if (relative != 0) {
if (addr + aliasBase < addr) {
log_error("\n%s: Relative offset overflow", argv[0]);
return CMD_EXIT_FAILURE;
}
addr += aliasBase;
}

if (phfs_aliasReg(argv[1], addr, sz) < 0) {
if (phfs_aliasReg(argv[optind], addr, sz) < 0) {
log_error("\nCan't register file %s", argv[1]);
return -EINVAL;
return CMD_EXIT_FAILURE;
}

log_info("\nRegistering file %s ", argv[1]);

return EOK;
return CMD_EXIT_SUCCESS;
}


Expand Down

0 comments on commit 9e07f5e

Please sign in to comment.