parent
832abd89bb
commit
37e46a66f3
|
@ -34,9 +34,9 @@
|
||||||
extern flash_info_t flash_info[]; /* info for FLASH chips */
|
extern flash_info_t flash_info[]; /* info for FLASH chips */
|
||||||
|
|
||||||
static int mac_location[] = {
|
static int mac_location[] = {
|
||||||
0x0, /* eth0 */
|
0x0, /* eth0 */
|
||||||
0x6, /* eth1 */
|
0x6, /* eth1 */
|
||||||
0x1002, /* wlan0 */
|
0x1002, /* wlan0 */
|
||||||
};
|
};
|
||||||
|
|
||||||
typedef unsigned char bool;
|
typedef unsigned char bool;
|
||||||
|
@ -45,282 +45,282 @@ typedef unsigned char bool;
|
||||||
|
|
||||||
/* from xyzmodem.c */
|
/* from xyzmodem.c */
|
||||||
// Validate a hex character
|
// Validate a hex character
|
||||||
__inline__ static bool
|
__inline__ static bool
|
||||||
_is_hex(char c)
|
_is_hex(char c)
|
||||||
{
|
{
|
||||||
return (((c >= '0') && (c <= '9')) ||
|
return (((c >= '0') && (c <= '9')) ||
|
||||||
((c >= 'A') && (c <= 'F')) ||
|
((c >= 'A') && (c <= 'F')) ||
|
||||||
((c >= 'a') && (c <= 'f')));
|
((c >= 'a') && (c <= 'f')));
|
||||||
}
|
}
|
||||||
|
|
||||||
/* from xyzmodem.c */
|
/* from xyzmodem.c */
|
||||||
// Convert a single hex nibble
|
// Convert a single hex nibble
|
||||||
__inline__ static u8
|
__inline__ static u8
|
||||||
_from_hex(char c)
|
_from_hex(char c)
|
||||||
{
|
{
|
||||||
u8 ret = 0;
|
u8 ret = 0;
|
||||||
|
|
||||||
if ((c >= '0') && (c <= '9')) {
|
if ((c >= '0') && (c <= '9')) {
|
||||||
ret = (c - '0');
|
ret = (c - '0');
|
||||||
} else if ((c >= 'a') && (c <= 'f')) {
|
} else if ((c >= 'a') && (c <= 'f')) {
|
||||||
ret = (c - 'a' + 0x0a);
|
ret = (c - 'a' + 0x0a);
|
||||||
} else if ((c >= 'A') && (c <= 'F')) {
|
} else if ((c >= 'A') && (c <= 'F')) {
|
||||||
ret = (c - 'A' + 0x0A);
|
ret = (c - 'A' + 0x0A);
|
||||||
}
|
}
|
||||||
return ret;
|
return ret;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
static int is_valid_mac_str(char *mac)
|
static int is_valid_mac_str(char *mac)
|
||||||
{
|
{
|
||||||
/* mac must look like "00:11:22:33:44:55" */
|
/* mac must look like "00:11:22:33:44:55" */
|
||||||
int i;
|
int i;
|
||||||
|
|
||||||
if (strlen(mac) < 17)
|
if (strlen(mac) < 17)
|
||||||
return 0;
|
return 0;
|
||||||
|
|
||||||
for (i=0; i <=15 ; i+=3) {
|
for (i=0; i <=15 ; i+=3) {
|
||||||
if (!_is_hex(mac[i]) || !_is_hex(mac[i+1]))
|
if (!_is_hex(mac[i]) || !_is_hex(mac[i+1]))
|
||||||
return 0;
|
return 0;
|
||||||
|
|
||||||
/* check for colons - last group doesn't end with a colon*/
|
|
||||||
if (i != 15 && mac[i+2] != ':')
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
return 1;
|
/* check for colons - last group doesn't end with a colon*/
|
||||||
|
if (i != 15 && mac[i+2] != ':')
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
return 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
static int parse_mac(u8 *dst, char *mac_str) {
|
static int parse_mac(u8 *dst, char *mac_str) {
|
||||||
int i;
|
int i;
|
||||||
int j;
|
int j;
|
||||||
|
|
||||||
if (!is_valid_mac_str(mac_str))
|
if (!is_valid_mac_str(mac_str))
|
||||||
return 1;
|
return 1;
|
||||||
|
|
||||||
/* j = dst offset
|
/* j = dst offset
|
||||||
* i = mac_str offset
|
* i = mac_str offset
|
||||||
*/
|
*/
|
||||||
for (i=0, j=0; i<= 15; i+=3, j++) {
|
for (i=0, j=0; i<= 15; i+=3, j++) {
|
||||||
dst[j] = _from_hex(mac_str[i]) << 4 | _from_hex(mac_str[i + 1]);
|
dst[j] = _from_hex(mac_str[i]) << 4 | _from_hex(mac_str[i + 1]);
|
||||||
}
|
}
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
/* copy calibration sector to the sector before calibration */
|
/* copy calibration sector to the sector before calibration */
|
||||||
static int backup_calibration(u8 *buffer)
|
static int backup_calibration(u8 *buffer)
|
||||||
{
|
{
|
||||||
int rc = -1;
|
int rc = -1;
|
||||||
|
|
||||||
/* read */
|
/* read */
|
||||||
memcpy(buffer, (void *)BOARDCAL, CFG_FLASH_SECTOR_SIZE);
|
memcpy(buffer, (void *)BOARDCAL, CFG_FLASH_SECTOR_SIZE);
|
||||||
|
|
||||||
/* erase */
|
/* erase */
|
||||||
rc = flash_erase(flash_info, CAL_SECTOR-1, CAL_SECTOR-1);
|
rc = flash_erase(flash_info, CAL_SECTOR-1, CAL_SECTOR-1);
|
||||||
if (rc) {
|
if (rc) {
|
||||||
printf("Backup failed because flash erase failed! rc %d\n", rc);
|
printf("Backup failed because flash erase failed! rc %d\n", rc);
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
/* cp */
|
/* cp */
|
||||||
rc = write_buff(flash_info, buffer, BOARDCAL-CFG_FLASH_SECTOR_SIZE, CFG_FLASH_SECTOR_SIZE);
|
rc = write_buff(flash_info, buffer, BOARDCAL-CFG_FLASH_SECTOR_SIZE, CFG_FLASH_SECTOR_SIZE);
|
||||||
if (rc) {
|
if (rc) {
|
||||||
printf("Backup failed because write to flash failed! rc %d\n", rc);
|
printf("Backup failed because write to flash failed! rc %d\n", rc);
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
/* compare */
|
/* compare */
|
||||||
if (memcmp(buffer, (void *)BOARDCAL-CFG_FLASH_SECTOR_SIZE, CFG_FLASH_SECTOR_SIZE)) {
|
if (memcmp(buffer, (void *)BOARDCAL-CFG_FLASH_SECTOR_SIZE, CFG_FLASH_SECTOR_SIZE)) {
|
||||||
printf("Backup failed. Read back different value!\n");
|
printf("Backup failed. Read back different value!\n");
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
int write_mac(u8 *buffer, u8 macs[ARRAY_SIZE(mac_location)][ETH_ALEN])
|
int write_mac(u8 *buffer, u8 macs[ARRAY_SIZE(mac_location)][ETH_ALEN])
|
||||||
{
|
{
|
||||||
int i;
|
int i;
|
||||||
int rc;
|
int rc;
|
||||||
|
|
||||||
/* read */
|
/* read */
|
||||||
memcpy(buffer, (void *)BOARDCAL, CFG_FLASH_SECTOR_SIZE);
|
memcpy(buffer, (void *)BOARDCAL, CFG_FLASH_SECTOR_SIZE);
|
||||||
|
|
||||||
/* set macs */
|
/* set macs */
|
||||||
for(i=0; i<ARRAY_SIZE(mac_location); i++) {
|
for(i=0; i<ARRAY_SIZE(mac_location); i++) {
|
||||||
memcpy(buffer + mac_location[i], macs[i], ETH_ALEN);
|
memcpy(buffer + mac_location[i], macs[i], ETH_ALEN);
|
||||||
}
|
}
|
||||||
|
|
||||||
/* erase */
|
/* erase */
|
||||||
rc = flash_erase(flash_info, CAL_SECTOR, CAL_SECTOR);
|
rc = flash_erase(flash_info, CAL_SECTOR, CAL_SECTOR);
|
||||||
if (rc) {
|
if (rc) {
|
||||||
printf("Write mac failed because flash_erase failed! rc %d\n", rc);
|
printf("Write mac failed because flash_erase failed! rc %d\n", rc);
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
/* write */
|
/* write */
|
||||||
rc = write_buff(flash_info, buffer, BOARDCAL, CFG_FLASH_SECTOR_SIZE);
|
rc = write_buff(flash_info, buffer, BOARDCAL, CFG_FLASH_SECTOR_SIZE);
|
||||||
if (rc) {
|
if (rc) {
|
||||||
printf("Write mac failed because write_buff failed! rc %d\n", rc);
|
printf("Write mac failed because write_buff failed! rc %d\n", rc);
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
int do_setmac(cmd_tbl_t *cmdtp, int flag, int argc, char *argv[])
|
int do_setmac(cmd_tbl_t *cmdtp, int flag, int argc, char *argv[])
|
||||||
{
|
{
|
||||||
u8 buffer[CFG_FLASH_SECTOR_SIZE];
|
u8 buffer[CFG_FLASH_SECTOR_SIZE];
|
||||||
u8 macs[ARRAY_SIZE(mac_location)][ETH_ALEN];
|
u8 macs[ARRAY_SIZE(mac_location)][ETH_ALEN];
|
||||||
int i;
|
int i;
|
||||||
bool do_backup = true;
|
bool do_backup = true;
|
||||||
|
|
||||||
/* valid arguments are:
|
/* valid arguments are:
|
||||||
* set_mac <mac0> <mac1> [..] or set_mac <mac0> <mac1> [..] nobackup
|
* set_mac <mac0> <mac1> [..] or set_mac <mac0> <mac1> [..] nobackup
|
||||||
* check if enough arguments given
|
* check if enough arguments given
|
||||||
*/
|
*/
|
||||||
if (argc != (ARRAY_SIZE(mac_location)+1) &&
|
if (argc != (ARRAY_SIZE(mac_location)+1) &&
|
||||||
argc != (ARRAY_SIZE(mac_location)+2))
|
argc != (ARRAY_SIZE(mac_location)+2))
|
||||||
return CMD_RET_FAILURE;
|
return CMD_RET_FAILURE;
|
||||||
|
|
||||||
/* check if nobackup was given */
|
/* check if nobackup was given */
|
||||||
if (argc == (ARRAY_SIZE(mac_location)+2)) {
|
if (argc == (ARRAY_SIZE(mac_location)+2)) {
|
||||||
if(!strncmp("nobackup", argv[argc-1], 8)) {
|
if(!strncmp("nobackup", argv[argc-1], 8)) {
|
||||||
do_backup = false;
|
do_backup = false;
|
||||||
} else {
|
} else {
|
||||||
printf("Unknown argument %s!\n", argv[argc-1]);
|
printf("Unknown argument %s!\n", argv[argc-1]);
|
||||||
return CMD_RET_FAILURE;
|
return CMD_RET_FAILURE;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/* all other arguments are mac address */
|
/* all other arguments are mac address */
|
||||||
for (i=0; i < ARRAY_SIZE(mac_location); i++) {
|
for (i=0; i < ARRAY_SIZE(mac_location); i++) {
|
||||||
if (!is_valid_mac_str(argv[i+1])) {
|
if (!is_valid_mac_str(argv[i+1])) {
|
||||||
printf("Invalid MAC address %s!\n", argv[i+1]);
|
printf("Invalid MAC address %s!\n", argv[i+1]);
|
||||||
return CMD_RET_FAILURE;
|
return CMD_RET_FAILURE;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/* parse macs and copy into macs */
|
/* parse macs and copy into macs */
|
||||||
for (i=0; i < ARRAY_SIZE(mac_location); i++) {
|
for (i=0; i < ARRAY_SIZE(mac_location); i++) {
|
||||||
if (parse_mac(macs[i], argv[i+1])) {
|
if (parse_mac(macs[i], argv[i+1])) {
|
||||||
printf("Can not parse mac %s!\n", argv[i+1]);
|
printf("Can not parse mac %s!\n", argv[i+1]);
|
||||||
return CMD_RET_FAILURE;
|
return CMD_RET_FAILURE;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (do_backup) {
|
if (do_backup) {
|
||||||
if (backup_calibration(buffer)) {
|
if (backup_calibration(buffer)) {
|
||||||
printf("Backup failed to the sector before calibration. Not updating calibration sector!");
|
printf("Backup failed to the sector before calibration. Not updating calibration sector!");
|
||||||
return CMD_RET_FAILURE;
|
return CMD_RET_FAILURE;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!write_mac(buffer, macs)) {
|
if (!write_mac(buffer, macs)) {
|
||||||
return CMD_RET_FAILURE;
|
return CMD_RET_FAILURE;
|
||||||
}
|
}
|
||||||
|
|
||||||
return CMD_RET_SUCCESS;
|
return CMD_RET_SUCCESS;
|
||||||
}
|
}
|
||||||
|
|
||||||
int do_showmac(cmd_tbl_t *cmdtp, int flag, int argc, char *argv[])
|
int do_showmac(cmd_tbl_t *cmdtp, int flag, int argc, char *argv[])
|
||||||
{
|
{
|
||||||
int i = 0;
|
int i = 0;
|
||||||
u8 buffer[CFG_FLASH_SECTOR_SIZE];
|
u8 buffer[CFG_FLASH_SECTOR_SIZE];
|
||||||
u8 *mac;
|
u8 *mac;
|
||||||
memcpy(buffer, (void *)BOARDCAL, CFG_FLASH_SECTOR_SIZE);
|
memcpy(buffer, (void *)BOARDCAL, CFG_FLASH_SECTOR_SIZE);
|
||||||
|
|
||||||
for (i=0; i < ARRAY_SIZE(mac_location); i++) {
|
for (i=0; i < ARRAY_SIZE(mac_location); i++) {
|
||||||
mac = buffer + mac_location[i];
|
mac = buffer + mac_location[i];
|
||||||
printf("mac %d = %02x:%02x:%02x:%02x:%02x:%02x\n", i,
|
printf("mac %d = %02x:%02x:%02x:%02x:%02x:%02x\n", i,
|
||||||
mac[0], mac[1], mac[2], mac[3], mac[4], mac[5]);
|
mac[0], mac[1], mac[2], mac[3], mac[4], mac[5]);
|
||||||
}
|
}
|
||||||
|
|
||||||
return CMD_RET_SUCCESS;
|
return CMD_RET_SUCCESS;
|
||||||
}
|
}
|
||||||
|
|
||||||
int do_setserial(cmd_tbl_t *cmdtp, int flag, int argc, char *argv[])
|
int do_setserial(cmd_tbl_t *cmdtp, int flag, int argc, char *argv[])
|
||||||
{
|
{
|
||||||
u8 buffer[CFG_FLASH_SECTOR_SIZE];
|
u8 buffer[CFG_FLASH_SECTOR_SIZE];
|
||||||
int32_t *serial;
|
int32_t *serial;
|
||||||
int32_t serial_number;
|
int32_t serial_number;
|
||||||
int rc;
|
int rc;
|
||||||
|
|
||||||
if (argc != 2)
|
if (argc != 2)
|
||||||
return CMD_RET_FAILURE;
|
return CMD_RET_FAILURE;
|
||||||
|
|
||||||
serial_number = simple_strtoul(argv[1], NULL, 16);
|
serial_number = simple_strtoul(argv[1], NULL, 16);
|
||||||
if (serial_number <= 0) {
|
if (serial_number <= 0) {
|
||||||
printf("Invalid serialnumber. <= 0!\n");
|
printf("Invalid serialnumber. <= 0!\n");
|
||||||
return CMD_RET_FAILURE;
|
return CMD_RET_FAILURE;
|
||||||
}
|
}
|
||||||
|
|
||||||
/* read */
|
/* read */
|
||||||
memcpy(buffer, (void *)BOARDCAL, CFG_FLASH_SECTOR_SIZE);
|
memcpy(buffer, (void *)BOARDCAL, CFG_FLASH_SECTOR_SIZE);
|
||||||
|
|
||||||
/* set serial number */
|
/* set serial number */
|
||||||
serial = (int32_t *) (buffer + SERIAL_LOCATION);
|
serial = (int32_t *) (buffer + SERIAL_LOCATION);
|
||||||
*serial = serial_number;
|
*serial = serial_number;
|
||||||
|
|
||||||
/* erase */
|
/* erase */
|
||||||
rc = flash_erase(flash_info, CAL_SECTOR, CAL_SECTOR);
|
rc = flash_erase(flash_info, CAL_SECTOR, CAL_SECTOR);
|
||||||
if (rc) {
|
if (rc) {
|
||||||
printf("Write serial failed because flash_erase failed! rc %d\n", rc);
|
printf("Write serial failed because flash_erase failed! rc %d\n", rc);
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
/* write */
|
/* write */
|
||||||
rc = write_buff(flash_info, buffer, BOARDCAL, CFG_FLASH_SECTOR_SIZE);
|
rc = write_buff(flash_info, buffer, BOARDCAL, CFG_FLASH_SECTOR_SIZE);
|
||||||
if (rc) {
|
if (rc) {
|
||||||
printf("Write serial failed because write_buff failed! rc %d\n", rc);
|
printf("Write serial failed because write_buff failed! rc %d\n", rc);
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
printf("Set serial to %d\n", serial_number);
|
printf("Set serial to %d\n", serial_number);
|
||||||
|
|
||||||
return CMD_RET_SUCCESS;
|
return CMD_RET_SUCCESS;
|
||||||
}
|
}
|
||||||
|
|
||||||
int do_showserial(cmd_tbl_t *cmdtp, int flag, int argc, char *argv[])
|
int do_showserial(cmd_tbl_t *cmdtp, int flag, int argc, char *argv[])
|
||||||
{
|
{
|
||||||
u8 buffer[CFG_FLASH_SECTOR_SIZE];
|
u8 buffer[CFG_FLASH_SECTOR_SIZE];
|
||||||
int32_t *serial;
|
int32_t *serial;
|
||||||
memcpy(buffer, (void *)BOARDCAL, CFG_FLASH_SECTOR_SIZE);
|
memcpy(buffer, (void *)BOARDCAL, CFG_FLASH_SECTOR_SIZE);
|
||||||
|
|
||||||
serial = (int32_t *) (buffer + SERIAL_LOCATION);
|
serial = (int32_t *) (buffer + SERIAL_LOCATION);
|
||||||
printf("serial %d\n", *serial);
|
printf("serial %d\n", *serial);
|
||||||
|
|
||||||
return CMD_RET_SUCCESS;
|
return CMD_RET_SUCCESS;
|
||||||
}
|
}
|
||||||
|
|
||||||
U_BOOT_CMD(
|
U_BOOT_CMD(
|
||||||
setmac, ARRAY_SIZE(mac_location)+2, 0, do_setmac,
|
setmac, ARRAY_SIZE(mac_location)+2, 0, do_setmac,
|
||||||
"setmac - Set ethernet MAC addresses\n",
|
"setmac - Set ethernet MAC addresses\n",
|
||||||
"setmac [<eth0> <eth1> <wlan0> [nobackup]]\n"
|
"setmac [<eth0> <eth1> <wlan0> [nobackup]]\n"
|
||||||
" without arguments it shows actual configuration\n"
|
" without arguments it shows actual configuration\n"
|
||||||
" <eth0> mac address\n"
|
" <eth0> mac address\n"
|
||||||
" <eth1> mac address\n"
|
" <eth1> mac address\n"
|
||||||
" <wlan0> mac address\n"
|
" <wlan0> mac address\n"
|
||||||
" mac address 00:aa:bb:cc:dd:ee\n"
|
" mac address 00:aa:bb:cc:dd:ee\n"
|
||||||
" nobackup - don't do a backup in the sector before calibration"
|
" nobackup - don't do a backup in the sector before calibration"
|
||||||
);
|
);
|
||||||
|
|
||||||
U_BOOT_CMD(
|
U_BOOT_CMD(
|
||||||
showmac, 1, 0, do_showmac,
|
showmac, 1, 0, do_showmac,
|
||||||
"showmac - Show ethernet MAC addresses\n",
|
"showmac - Show ethernet MAC addresses\n",
|
||||||
);
|
);
|
||||||
|
|
||||||
U_BOOT_CMD(
|
U_BOOT_CMD(
|
||||||
setserial, 2, 0, do_setserial,
|
setserial, 2, 0, do_setserial,
|
||||||
"setserial - Set the serial number\n",
|
"setserial - Set the serial number\n",
|
||||||
"setserial serial\n"
|
"setserial serial\n"
|
||||||
);
|
);
|
||||||
|
|
||||||
U_BOOT_CMD(
|
U_BOOT_CMD(
|
||||||
showserial, 1, 0, do_showserial,
|
showserial, 1, 0, do_showserial,
|
||||||
"showserial - Show serial number\n",
|
"showserial - Show serial number\n",
|
||||||
);
|
);
|
||||||
|
|
||||||
|
|
||||||
#endif /* CFG_CMD_SETMAC */
|
#endif /* CFG_CMD_SETMAC */
|
||||||
|
|
Loading…
Reference in New Issue