X-Git-Url: https://repo.jachan.dev/u-boot.git/blobdiff_plain/cbe607b920bc0827d8fe379ed4f5ae4e2058513e..03de305ec48b0bb28554372abb40ccd46dbe0bf9:/cmd/tlv_eeprom.c diff --git a/cmd/tlv_eeprom.c b/cmd/tlv_eeprom.c index 5099cbdd95..0aec752177 100644 --- a/cmd/tlv_eeprom.c +++ b/cmd/tlv_eeprom.c @@ -9,7 +9,6 @@ * Copyright (C) 2014,2016 david_yang */ -#include #include #include #include @@ -29,26 +28,23 @@ DECLARE_GLOBAL_DATA_PTR; /* File scope function prototypes */ static bool is_checksum_valid(u8 *eeprom); -static int read_eeprom(u8 *eeprom); -static void show_eeprom(u8 *eeprom); +static int read_eeprom(int devnum, u8 *eeprom); +static void show_eeprom(int devnum, u8 *eeprom); static void decode_tlv(struct tlvinfo_tlv *tlv); static void update_crc(u8 *eeprom); -static int prog_eeprom(u8 *eeprom); +static int prog_eeprom(int devnum, u8 *eeprom); static bool tlvinfo_find_tlv(u8 *eeprom, u8 tcode, int *eeprom_index); static bool tlvinfo_delete_tlv(u8 *eeprom, u8 code); static bool tlvinfo_add_tlv(u8 *eeprom, int tcode, char *strval); static int set_mac(char *buf, const char *string); static int set_date(char *buf, const char *string); static int set_bytes(char *buf, const char *string, int *converted_accum); -static void show_tlv_devices(void); +static void show_tlv_devices(int current_dev); -/* Set to 1 if we've read EEPROM into memory */ -static int has_been_read; /* The EERPOM contents after being read into memory */ static u8 eeprom[TLV_INFO_MAX_LEN]; static struct udevice *tlv_devices[MAX_TLV_DEVICES]; -static unsigned int current_dev; #define to_header(p) ((struct tlvinfo_header *)p) #define to_entry(p) ((struct tlvinfo_tlv *)p) @@ -125,22 +121,20 @@ static bool is_checksum_valid(u8 *eeprom) * * Read the EEPROM into memory, if it hasn't already been read. */ -static int read_eeprom(u8 *eeprom) +static int read_eeprom(int devnum, u8 *eeprom) { int ret; struct tlvinfo_header *eeprom_hdr = to_header(eeprom); struct tlvinfo_tlv *eeprom_tlv = to_entry(&eeprom[HDR_SIZE]); - if (has_been_read) - return 0; - /* Read the header */ - ret = read_tlv_eeprom((void *)eeprom_hdr, 0, HDR_SIZE, current_dev); + ret = read_tlv_eeprom((void *)eeprom_hdr, 0, HDR_SIZE, devnum); /* If the header was successfully read, read the TLVs */ if (ret == 0 && is_valid_tlvinfo_header(eeprom_hdr)) ret = read_tlv_eeprom((void *)eeprom_tlv, HDR_SIZE, - be16_to_cpu(eeprom_hdr->totallen), - current_dev); + be16_to_cpu(eeprom_hdr->totallen), devnum); + else if (ret == -ENODEV) + return ret; // If the contents are invalid, start over with default contents if (!is_valid_tlvinfo_header(eeprom_hdr) || @@ -151,10 +145,8 @@ static int read_eeprom(u8 *eeprom) update_crc(eeprom); } - has_been_read = 1; - #ifdef DEBUG - show_eeprom(eeprom); + show_eeprom(devnum, eeprom); #endif return ret; @@ -165,10 +157,13 @@ static int read_eeprom(u8 *eeprom) * * Display the contents of the EEPROM */ -static void show_eeprom(u8 *eeprom) +static void show_eeprom(int devnum, u8 *eeprom) { int tlv_end; int curr_tlv; +#ifdef DEBUG + int i; +#endif struct tlvinfo_header *eeprom_hdr = to_header(eeprom); struct tlvinfo_tlv *eeprom_tlv; @@ -177,7 +172,7 @@ static void show_eeprom(u8 *eeprom) return; } - printf("TLV: %u\n", current_dev); + printf("TLV: %u\n", devnum); printf("TlvInfo Header:\n"); printf(" Id String: %s\n", eeprom_hdr->signature); printf(" Version: %d\n", eeprom_hdr->version); @@ -386,7 +381,7 @@ static void update_crc(u8 *eeprom) * * Write the EEPROM data from CPU memory to the hardware. */ -static int prog_eeprom(u8 *eeprom) +static int prog_eeprom(int devnum, u8 *eeprom) { int ret = 0; struct tlvinfo_header *eeprom_hdr = to_header(eeprom); @@ -395,7 +390,7 @@ static int prog_eeprom(u8 *eeprom) update_crc(eeprom); eeprom_len = HDR_SIZE + be16_to_cpu(eeprom_hdr->totallen); - ret = write_tlv_eeprom(eeprom, eeprom_len); + ret = write_tlv_eeprom(eeprom, eeprom_len, devnum); if (ret) { printf("Programming failed.\n"); return -1; @@ -430,11 +425,23 @@ int do_tlv_eeprom(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[]) { char cmd; struct tlvinfo_header *eeprom_hdr = to_header(eeprom); + static unsigned int current_dev; + /* Set to 1 if we've read EEPROM into memory */ + static int has_been_read; + int ret; // If no arguments, read the EERPOM and display its contents if (argc == 1) { - read_eeprom(eeprom); - show_eeprom(eeprom); + if (!has_been_read) { + ret = read_eeprom(current_dev, eeprom); + if (ret) { + printf("Failed to read EEPROM data from device.\n"); + return 0; + } + + has_been_read = 1; + } + show_eeprom(current_dev, eeprom); return 0; } @@ -442,12 +449,33 @@ int do_tlv_eeprom(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[]) // "reset" will both be treated as "read". cmd = argv[1][0]; + // select device + if (cmd == 'd') { + /* 'dev' command */ + unsigned int devnum; + + devnum = simple_strtoul(argv[2], NULL, 0); + if (devnum >= MAX_TLV_DEVICES) { + printf("Invalid device number\n"); + return 0; + } + current_dev = devnum; + has_been_read = 0; + + return 0; + } + // Read the EEPROM contents if (cmd == 'r') { has_been_read = 0; - if (!read_eeprom(eeprom)) - printf("EEPROM data loaded from device to memory.\n"); - return 0; + ret = read_eeprom(current_dev, eeprom); + if (ret) { + printf("Failed to read EEPROM data from device.\n"); + return 0; + } + + printf("EEPROM data loaded from device to memory.\n"); + has_been_read = 1; } // Subsequent commands require that the EEPROM has already been read. @@ -460,7 +488,7 @@ int do_tlv_eeprom(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[]) if (argc == 2) { switch (cmd) { case 'w': /* write */ - prog_eeprom(eeprom); + prog_eeprom(current_dev, eeprom); break; case 'e': /* erase */ strcpy(eeprom_hdr->signature, TLV_INFO_ID_STRING); @@ -473,20 +501,17 @@ int do_tlv_eeprom(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[]) show_tlv_code_list(); break; case 'd': /* dev */ - show_tlv_devices(); + show_tlv_devices(current_dev); break; default: - cmd_usage(cmdtp); - break; + return CMD_RET_USAGE; } return 0; } // The set command takes one or two args. - if (argc > 4) { - cmd_usage(cmdtp); - return 0; - } + if (argc > 4) + return CMD_RET_USAGE; // Set command. If the TLV exists in the EEPROM, delete it. Then if // data was supplied for this TLV add the TLV with the new contents at @@ -498,18 +523,8 @@ int do_tlv_eeprom(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[]) tlvinfo_delete_tlv(eeprom, tcode); if (argc == 4) tlvinfo_add_tlv(eeprom, tcode, argv[3]); - } else if (cmd == 'd') { /* 'dev' command */ - unsigned int devnum; - - devnum = simple_strtoul(argv[2], NULL, 0); - if (devnum > MAX_TLV_DEVICES || !tlv_devices[devnum]) { - printf("Invalid device number\n"); - return 0; - } - current_dev = devnum; - has_been_read = 0; } else { - cmd_usage(cmdtp); + return CMD_RET_USAGE; } return 0; @@ -751,7 +766,7 @@ static int set_mac(char *buf, const char *string) /* Convert string to binary */ for (i = 0, p = (char *)string; i < 6; i++) { - buf[i] = p ? simple_strtoul(p, &end, 16) : 0; + buf[i] = p ? hextoul(p, &end) : 0; if (p) p = (*end) ? end + 1 : end; } @@ -883,7 +898,7 @@ static int set_bytes(char *buf, const char *string, int *converted_accum) return 0; } -static void show_tlv_devices(void) +static void show_tlv_devices(int current_dev) { unsigned int dev; @@ -953,14 +968,14 @@ int read_tlv_eeprom(void *eeprom, int offset, int len, int dev_num) /** * write_tlv_eeprom - write the hwinfo to i2c EEPROM */ -int write_tlv_eeprom(void *eeprom, int len) +int write_tlv_eeprom(void *eeprom, int len, int dev) { if (!(gd->flags & GD_FLG_RELOC)) return -ENODEV; - if (!tlv_devices[current_dev]) + if (!tlv_devices[dev]) return -ENODEV; - return i2c_eeprom_write(tlv_devices[current_dev], 0, eeprom, len); + return i2c_eeprom_write(tlv_devices[dev], 0, eeprom, len); } int read_tlvinfo_tlv_eeprom(void *eeprom, struct tlvinfo_header **hdr, @@ -1015,10 +1030,11 @@ int mac_read_from_eeprom(void) int maccount; u8 macbase[6]; struct tlvinfo_header *eeprom_hdr = to_header(eeprom); + int devnum = 0; // TODO: support multiple EEPROMs puts("EEPROM: "); - if (read_eeprom(eeprom)) { + if (read_eeprom(devnum, eeprom)) { printf("Read failed.\n"); return -1; } @@ -1071,19 +1087,7 @@ int mac_read_from_eeprom(void) return 0; } -/** - * populate_serial_number - read the serial number from EEPROM - * - * This function reads the serial number from the EEPROM and sets the - * appropriate environment variable. - * - * The environment variable is only set if it has not been set - * already. This ensures that any user-saved variables are never - * overwritten. - * - * This function must be called after relocation. - */ -int populate_serial_number(void) +int serial_read_from_eeprom(int devnum) { char serialstr[257]; int eeprom_index; @@ -1092,7 +1096,7 @@ int populate_serial_number(void) if (env_get("serial#")) return 0; - if (read_eeprom(eeprom)) { + if (read_eeprom(devnum, eeprom)) { printf("Read failed.\n"); return -1; }