Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

alias: Add option to use relative alias offset #310

Merged
merged 1 commit into from
Dec 27, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
93 changes: 74 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,88 @@ 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) {
if (aliasBase != 0) {
lib_printf("\nRelative alias base: 0x%p", aliasBase);
}
phfs_aliasesShow();
return EOK;
return CMD_EXIT_SUCCESS;
}

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

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

case 'b':
agkaminski marked this conversation as resolved.
Show resolved Hide resolved
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;
lib_printf("\n%s: Setting relative base address to 0x%p", argv[0], newBase);

agkaminski marked this conversation as resolved.
Show resolved Hide resolved
/* 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;
}
}
else if (argc != 4) {
log_error("\n%s: Wrong argument count", argv[0]);
return -EINVAL;

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

addr = lib_strtoul(argv[optind + 1], &end, 0);
if (*end != '\0') {
log_error("\n%s: Wrong arguments", 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);
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
Loading