Antonio Di Bacco wrote:
I don't manage to do some simple calculations like:
echo $((1+1))
Bye,
Antonio
I've solved this with a new command 'eval' which is used to set a
variable based on an expression. For example, I wanted to determine the
start address of the kernel in flash for both 4 and 8meg parts but since
its sits above U-Boot in the memory map (and U-Boot has to be 1M down
from the top for high boot), it has to be calculated depending on the
flash size. In this case, the new variable 'kb' contains the address of
the beginning of the kernel of either 40340000 or 40740000 for either 4M
or 8M devices.
"eval kb $flash - c00c0000;" \
This is work I did a couple of years ago and I'm no longer on the
project so I'm not able to provide a 'formal' patch. To use it, put the
file cmd_eval.c into ...uboot/common/ and make the following mods
in cmd_confdefs.h add
#define CFG_CMD_EVAL 0x0080000000000000U /* Eval arithmetics */
in your board config add
#define CONFIG_COMMANDS ( ( CONFIG_CMD_DFL \
...............
| CFG_CMD_EVAL \
in ..../uboot/common/Makefile
COBJS = main.o ACEX1K.o altera.o bedbug.o \
............
cmd_eval.o
And that should be it.
I hope it helps...
--
Robin Gilks
Senior Design Engineer Phone: (+64)(3) 357 1569
Tait Electronics Fax : (+64)(3) 359 4632
PO Box 1645 Christchurch Email : robin.gilks@tait.co.nz
New Zealand
=======================================================================
This email, including any attachments, is only for the intended
addressee. It is subject to copyright, is confidential and may be
the subject of legal or other privilege, none of which is waived or
lost by reason of this transmission.
If the receiver is not the intended addressee, please accept our
apologies, notify us by return, delete all copies and perform no
other act on the email.
Unfortunately, we cannot warrant that the email has not been
altered or corrupted during transmission.
=======================================================================
/*
* (C) Copyright 2003
* Tait Electronics Limited, Christchurch, New Zealand
*
* See file CREDITS for list of people who contributed to this
* project.
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License as
* published by the Free Software Foundation; either version 2 of
* the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston,
* MA 02111-1307 USA
*/
/*
* This file provides an 'eval' function. It is very crude and can only set a variable, not pass
* the result directly back to an inline calculation.
* It supports binary operators +, -, *, /, &, |
*/
#include <common.h>
#include <config.h>
#include <command.h>
#if (CONFIG_COMMANDS & CFG_CMD_EVAL)
#define PLUS 0
#define MINUS 1
#define TIMES 2
#define DIVIDE 3
#define LAND 4
#define LOR 5
#define LXOR 6
static struct op_tbl_s {
char *op; /* operator string */
int opcode; /* internal representation of opcode */
};
typedef struct op_tbl_s op_tbl_t;
static op_tbl_t op_table [] = {
{"+", PLUS},
{"-", MINUS},
{"*", TIMES},
{"/", DIVIDE},
{"&", LAND},
{"|", LOR},
{"^", LXOR},
};
#define op_tbl_size (sizeof(op_table)/sizeof(op_table[0]))
extern int cmd_get_data_size(char* arg, int default_size);
static long evalexp(char *s, int w)
{
long l, *p;
/* if the parameter starts with a * then assume is a pointer to the value we want */
if (s[0] == '*') {
p = (long *)simple_strtoul(&s[1], NULL, 16);
l = *p;
} else {
l = simple_strtoul(s, NULL, 16);
}
return (l & ((1 << (w * 8)) - 1));
}
static int arith (char *s, char *t, int op, int w)
{
long l, r;
l = evalexp (s, w);
r = evalexp (t, w);
switch (op) {
case PLUS: return (l + r);
case MINUS: return (l - r);
case TIMES: return (l * r);
case DIVIDE: return (l / r);
case LAND: return (l & r);
case LOR: return (l | r);
case LXOR: return (l ^ r);
}
return (0);
}
/* command line interface to the shell test */
int do_eval ( cmd_tbl_t *cmdtp, int flag, int argc, char *argv[] )
{
int w, len, i;
op_tbl_t *optp;
char *op, buf[32];
long value;
/* Validate arguments */
if ((argc != 5)){
printf("Usage:\n%s\n", cmdtp->usage);
return 1;
}
/* Check for a data width specification.
* Defaults to long (4) if no specification.
* Uses -2 as 'width' for .s (string) so as not to upset existing code
*/
switch (w = cmd_get_data_size(argv[0], 4)) {
case 1:
case 2:
case 4:
op = argv[3];
len = strlen(op);
for (optp = (op_tbl_t *)&op_table, i = 0; i < op_tbl_size; optp++, i++) {
if ((strncmp (op, optp->op, len) == 0) && (len == strlen (optp->op))) {
value = arith(argv[2], argv[4], optp->opcode, w);
sprintf(buf, "%lx", value);
setenv(argv[1], buf);
return 0;
}
}
printf("Unknown operator '%s'\n", op);
break;
case -1:
puts("Invalid data width specifier\n");
break;
}
return 0;
}
U_BOOT_CMD(
eval, 5, 0, do_eval,
"eval - set var from arithmetic operation\n",
"[.b, .w, .l] var [*]value1 <op> [*]value2\n"
);
#endif /* CONFIG_CMD_EVAL */
=======================================================================
This email, including any attachments, is only for the intended
addressee. It is subject to copyright, is confidential and may be
the subject of legal or other privilege, none of which is waived or
lost by reason of this transmission.
If the receiver is not the intended addressee, please accept our
apologies, notify us by return, delete all copies and perform no
other act on the email.
Unfortunately, we cannot warrant that the email has not been
altered or corrupted during transmission.
=======================================================================