load_sernum_ethaddr.c 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. /*
  2. * (C) Copyright 2000, 2001, 2002
  3. * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
  4. *
  5. * SPDX-License-Identifier: GPL-2.0+
  6. */
  7. #include <common.h>
  8. #include <mpc8xx.h>
  9. /*-----------------------------------------------------------------------
  10. * Process Hardware Information Block:
  11. *
  12. * If we boot on a system fresh from factory, check if the Hardware
  13. * Information Block exists and save the information it contains.
  14. *
  15. * The TQM8xxL / TQM82xx Hardware Information Block is defined as
  16. * follows:
  17. * - located in first flash bank
  18. * - starts at offset 0x0003FFC0
  19. * - size 0x00000040
  20. *
  21. * Internal structure:
  22. * - sequence of ASCII character strings
  23. * - fields separated by a single space character (0x20)
  24. * - last field terminated by NUL character (0x00)
  25. * - remaining space filled with NUL characters (0x00)
  26. *
  27. * Fields in Hardware Information Block:
  28. * 1) Module Type
  29. * 2) Serial Number
  30. * 3) First MAC Address
  31. * 4) Number of additional MAC addresses
  32. */
  33. void load_sernum_ethaddr (void)
  34. {
  35. unsigned char *hwi;
  36. unsigned char serial [CONFIG_SYS_HWINFO_SIZE];
  37. unsigned char ethaddr[CONFIG_SYS_HWINFO_SIZE];
  38. unsigned short ih, is, ie, part;
  39. hwi = (unsigned char *)(CONFIG_SYS_FLASH_BASE + CONFIG_SYS_HWINFO_OFFSET);
  40. ih = is = ie = 0;
  41. if (*((unsigned long *)hwi) != (unsigned long)CONFIG_SYS_HWINFO_MAGIC) {
  42. return;
  43. }
  44. part = 1;
  45. /* copy serial # / MAC address */
  46. while ((hwi[ih] != '\0') && (ih < CONFIG_SYS_HWINFO_SIZE)) {
  47. if (hwi[ih] < ' ' || hwi[ih] > '~') { /* ASCII strings! */
  48. return;
  49. }
  50. switch (part) {
  51. default: /* Copy serial # */
  52. if (hwi[ih] == ' ') {
  53. ++part;
  54. }
  55. serial[is++] = hwi[ih];
  56. break;
  57. case 3: /* Copy MAC address */
  58. if (hwi[ih] == ' ') {
  59. ++part;
  60. break;
  61. }
  62. ethaddr[ie++] = hwi[ih];
  63. if ((ie % 3) == 2)
  64. ethaddr[ie++] = ':';
  65. break;
  66. }
  67. ++ih;
  68. }
  69. serial[is] = '\0';
  70. if (ie && ethaddr[ie-1] == ':')
  71. --ie;
  72. ethaddr[ie] = '\0';
  73. /* set serial# and ethaddr if not yet defined */
  74. if (getenv("serial#") == NULL) {
  75. setenv ((char *)"serial#", (char *)serial);
  76. }
  77. if (getenv("ethaddr") == NULL) {
  78. setenv ((char *)"ethaddr", (char *)ethaddr);
  79. }
  80. }