xilinx: board: Improve legacy format handling
Michal Simek
maintainerv2 cover xilinx: board: Improve legacy format handling
Hi,
fix and improve legacy format handling to cover cases where eeprom content is corrupted and random. Very likely detection algorithm can be improved - for example check that mac address is valid, check all strings, etc. but the aim of this series is to remove all non printable chars without mac address and never copy more bytes than expected.
Thanks,
Michal
Changes in v2:
- Add +1 for strlcpy because only size -1 is used compare to strncpy.
Michal Simek (3):
xilinx: board: Use ETH_ALEN macro for mac address size
xilinx: board: Fix xilinx_eeprom_legacy_cleanup()
xilinx: board: Update logic in xilinx_read_eeprom_legacy
board/xilinx/common/board.c | 34 +++++++++++++++++++++------------- 1 file changed, 21 insertions(+), 13 deletions(-)
Signature · 1 line
2.36.1
Michal Simek
maintainerPATCH v2 1/3 xilinx: board: Use ETH_ALEN macro for mac address size
Use predefined macro for eth_mac legacy format.
---
(no changes since v1)
board/xilinx/common/board.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
board/xilinx/common/board.c +1 -1
@@ -85,7 +85,7 @@ static struct xilinx_board_description *board_info;struct xilinx_legacy_format {char board_sn[18 ];/* 0x0 */ char unused0[14 ];/* 0x12 */ -char eth_mac[6 ];/* 0x20 */ +char eth_mac[ETH_ALEN ];/* 0x20 */ char unused1[170 ];/* 0x26 */ char board_name[11 ];/* 0xd0 */ char unused2[5 ];/* 0xdc */
Signature · 1 line
2.36.1
Michal Simek
maintainerPATCH v2 2/3 xilinx: board: Fix xilinx_eeprom_legacy_cleanup()
In reply to Michal SimekWhen ethernet mac address contains 0x20 or 0xff MAC address is changed and bytes are converted to zeros. That's why fix decoding algorithm to ignore fields where MAC address is stored and all non printable chars (including space) are zeroed.
---
(no changes since v1)
board/xilinx/common/board.c | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-)
board/xilinx/common/board.c +6 -2
@@ -101,9 +101,13 @@ static void xilinx_eeprom_legacy_cleanup(char *eeprom, int size)for (i =0 ; i < size; i++) { byte = eeprom[i]; -/* Remove all ffs and spaces */ -if (byte ==0xff || byte ==' ' ) +/* Remove all non printable chars but ignore MAC address */ +if ((i < offsetof(struct xilinx_legacy_format , eth_mac) || + i >= offsetof(struct xilinx_legacy_format , unused1)) && + (byte <'!' || byte >'~' )) { eeprom[i] =0 ; +continue ; + }/* Convert strings to lower case */ if (byte >='A' && byte <='Z' )
Signature · 1 line
2.36.1
Michal Simek
maintainerPATCH v2 3/3 xilinx: board: Update logic in xilinx_read_eeprom_legacy
In reply to Michal SimekWhen eeprom has random content printing random chars can stuck U-Boot. That's why update legacy eeprom format decoding algorithm to copy only maximum amount of chars allocated for fields.
And also print them directly from desc structure.
Previous algorithm was printing strings first directly from eeprom content and then copy them to desc structure.
---
Changes in v2:
- Add +1 for strlcpy because only size -1 is used compare to strncpy.
board/xilinx/common/board.c | 24 ++++++++++++++---------- 1 file changed, 14 insertions(+), 10 deletions(-)
board/xilinx/common/board.c +14 -10
@@ -140,21 +140,25 @@ static int xilinx_read_eeprom_legacy(struct udevice *dev, char *name,xilinx_eeprom_legacy_cleanup ((char *)eeprom_content, size); -printf ("Xilinx I2C Legacy format at %s:\n" , name); -printf (" Board name:\t%s\n" , eeprom_content->board_name); -printf (" Board rev:\t%s\n" , eeprom_content->board_revision); -printf (" Board SN:\t%s\n" , eeprom_content->board_sn); +/* Terminating \0 chars are the part of desc fields already */ +strlcpy (desc->name, eeprom_content->board_name, +sizeof (eeprom_content->board_name) +1 ); +strlcpy (desc->revision, eeprom_content->board_revision, +sizeof (eeprom_content->board_revision) +1 ); +strlcpy (desc->serial, eeprom_content->board_sn, +sizeof (eeprom_content->board_sn) +1 ); eth_valid =is_valid_ethaddr ((const u8 *)eeprom_content->eth_mac);if (eth_valid) -printf (" Ethernet mac:\t%pM\n" , eeprom_content->eth_mac); +memcpy (desc->mac_addr[0 ], eeprom_content->eth_mac,ETH_ALEN ); + +printf ("Xilinx I2C Legacy format at %s:\n" , name); +printf (" Board name:\t%s\n" , desc->name); +printf (" Board rev:\t%s\n" , desc->revision); +printf (" Board SN:\t%s\n" , desc->serial); -/* Terminating \0 chars ensure end of string */ -strcpy (desc->name, eeprom_content->board_name); -strcpy (desc->revision, eeprom_content->board_revision); -strcpy (desc->serial, eeprom_content->board_sn);if (eth_valid) -memcpy (desc->mac_addr[0 ], eeprom_content->eth_mac,ETH_ALEN ); +printf (" Ethernet mac:\t%pM\n" , desc->mac_addr); desc->header =EEPROM_HEADER_MAGIC ;
Signature · 1 line
2.36.1
Quoted 14 lines · Michal Simek
Hi,
fix and improve legacy format handling to cover cases where eeprom content is corrupted and random. Very likely detection algorithm can be improved - for example check that mac address is valid, check all strings, etc. but the aim of this series is to remove all non printable chars without mac address and never copy more bytes than expected.
Thanks,
MichalChanges in v2:
- Add +1 for strlcpy because only size -1 is used compare to strncpy.
Quoted 8 lines
Michal Simek (3):
xilinx: board: UseETH_ALENmacro for mac address size
xilinx: board: Fixxilinx_eeprom_legacy_cleanup()
xilinx: board: Update logic inxilinx_read_eeprom_legacyboard/xilinx/common/board.c | 34 +++++++++++++++++++++------------- 1 file changed, 21 insertions(+), 13 deletions(-)
Applied.
M