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