Improve the process for GNU tools
[minix3.git] / minix / usr.bin / eepromread / board_info.c
blob4f02c53ec0cf97d4a14c3e3c1d0858d117677295
1 #include <minix/i2c.h>
3 #include <sys/types.h>
4 #include <sys/stat.h>
6 #include <errno.h>
7 #include <fcntl.h>
8 #include <stdio.h>
9 #include <stdlib.h>
10 #include <string.h>
11 #include <unistd.h>
13 #include "eepromread.h"
16 * Attempt to read the board info from an eeprom on this board.
19 static int board_info_beaglebone(int fd, i2c_addr_t address, int flags,
20 enum device_types device_type);
21 static int board_info_cape_a0(int fd, i2c_addr_t address, int flags,
22 enum device_types device_type);
23 static int board_info_cape_a1(int fd, i2c_addr_t address, int flags,
24 enum device_types device_type);
26 /* Memory Layout of the BeagleBone and BeagleBone Black EEPROM */
27 typedef struct beaglebone_info
29 uint8_t magic_number[4]; /* Should be 0xaa 0x55 0x33 0xee */
30 char board_name[8]; /* Warning: strings not NULL terminated */
31 char version[4];
32 char serial_number[12];
33 char config[32]; /* All 0x00 on BeagleBone White */
34 char mac_addrs[3][6]; /* Not set on BeagleBone White */
35 } beaglebone_info_t;
37 /* Memory Layout of the Cape EEPROM (A0 Format) - Defined in BBW SRM Rev A5.3 */
38 typedef struct cape_info_a0
40 uint8_t magic_number[4]; /* Should be 0xaa 0x55 0x33 0xee */
41 char revision[2]; /* Should be 'A' '0' */
42 char board_name[32]; /* Warning: strings not NULL terminated */
43 char version[4];
44 char manufacturer[16];
45 char partno[16];
46 char num_pins[2];
47 char serial[12];
48 char pin_usage[140];
49 char vdd_3v3exp_i[2];
50 char vdd_5v_i[2];
51 char sys_5v_i[2];
52 char dc_supp[2];
53 } cape_info_a0_t;
55 /* Memory Layout of the Cape EEPROM (A1 Format) - Defined in BBB SRM Rev A5.3 */
56 typedef struct cape_info_a1
58 uint8_t magic_number[4]; /* Should be 0xaa 0x55 0x33 0xee */
59 char revision[2]; /* Should be 'A' '1' */
60 char board_name[32]; /* Warning: strings not NULL terminated */
61 char version[4];
62 char manufacturer[16];
63 char partno[16];
64 char num_pins[2];
65 char serial[12];
66 char pin_usage[148];
67 char vdd_3v3exp_i[2];
68 char vdd_5v_i[2];
69 char sys_5v_i[2];
70 char dc_supp[2];
71 } cape_info_a1_t;
73 static int
74 board_info_beaglebone(int fd, i2c_addr_t address, int flags,
75 enum device_types device_type)
77 int r;
78 int i, j;
79 char s[33];
80 beaglebone_info_t boneinfo;
82 r = eeprom_read(fd, address, 0x0000, &boneinfo,
83 sizeof(beaglebone_info_t), flags, device_type);
84 if (r == -1) {
85 fprintf(stderr, "Failed to read BeagleBone info r=%d\n", r);
86 return -1;
89 fprintf(stdout, "%-16s: 0x%x%x%x%x\n", "MAGIC_NUMBER",
90 boneinfo.magic_number[0], boneinfo.magic_number[1],
91 boneinfo.magic_number[2], boneinfo.magic_number[3]);
93 memset(s, '\0', 33);
94 memcpy(s, boneinfo.board_name, 8);
95 fprintf(stdout, "%-16s: %s\n", "BOARD_NAME", s);
97 memset(s, '\0', 33);
98 memcpy(s, boneinfo.version, 4);
99 fprintf(stdout, "%-16s: %s\n", "VERSION", s);
101 memset(s, '\0', 33);
102 memcpy(s, boneinfo.serial_number, 12);
103 fprintf(stdout, "%-16s: %s\n", "SERIAL_NUMBER", s);
105 return 0;
108 static int
109 board_info_cape_a0(int fd, i2c_addr_t address, int flags,
110 enum device_types device_type)
112 int r;
113 int i, j;
114 char s[33];
115 cape_info_a0_t capeinfo;
117 r = eeprom_read(fd, address, 0x0000, &capeinfo,
118 sizeof(cape_info_a0_t), flags, device_type);
119 if (r == -1) {
120 fprintf(stderr, "failed to read cape A0 info r=%d\n", r);
121 return -1;
124 fprintf(stdout, "%-16s: 0x%x%x%x%x\n", "MAGIC_NUMBER",
125 capeinfo.magic_number[0], capeinfo.magic_number[1],
126 capeinfo.magic_number[2], capeinfo.magic_number[3]);
128 memset(s, '\0', 33);
129 memcpy(s, capeinfo.revision, 2);
130 fprintf(stdout, "%-16s: %s\n", "REVISION", s);
132 memset(s, '\0', 33);
133 memcpy(s, capeinfo.board_name, 32);
134 fprintf(stdout, "%-16s: %s\n", "BOARD_NAME", s);
136 memset(s, '\0', 33);
137 memcpy(s, capeinfo.version, 4);
138 fprintf(stdout, "%-16s: %s\n", "VERSION", s);
140 memset(s, '\0', 33);
141 memcpy(s, capeinfo.manufacturer, 16);
142 fprintf(stdout, "%-16s: %s\n", "MANUFACTURER", s);
144 memset(s, '\0', 33);
145 memcpy(s, capeinfo.partno, 16);
146 fprintf(stdout, "%-16s: %s\n", "PART_NUMBER", s);
148 memset(s, '\0', 33);
149 memcpy(s, capeinfo.serial, 12);
150 fprintf(stdout, "%-16s: %s\n", "SERIAL", s);
152 return 0;
155 static int
156 board_info_cape_a1(int fd, i2c_addr_t address, int flags,
157 enum device_types device_type)
159 int r;
160 int i, j;
161 char s[33];
162 cape_info_a1_t capeinfo;
164 r = eeprom_read(fd, address, 0x0000, &capeinfo,
165 sizeof(cape_info_a1_t), flags, device_type);
166 if (r == -1) {
167 fprintf(stderr, "failed to read cape A0 info r=%d\n", r);
168 return -1;
171 fprintf(stdout, "%-16s: 0x%x%x%x%x\n", "MAGIC_NUMBER",
172 capeinfo.magic_number[0], capeinfo.magic_number[1],
173 capeinfo.magic_number[2], capeinfo.magic_number[3]);
175 memset(s, '\0', 33);
176 memcpy(s, capeinfo.revision, 2);
177 fprintf(stdout, "%-16s: %s\n", "REVISION", s);
179 memset(s, '\0', 33);
180 memcpy(s, capeinfo.board_name, 32);
181 fprintf(stdout, "%-16s: %s\n", "BOARD_NAME", s);
183 memset(s, '\0', 33);
184 memcpy(s, capeinfo.version, 4);
185 fprintf(stdout, "%-16s: %s\n", "VERSION", s);
187 memset(s, '\0', 33);
188 memcpy(s, capeinfo.manufacturer, 16);
189 fprintf(stdout, "%-16s: %s\n", "MANUFACTURER", s);
191 memset(s, '\0', 33);
192 memcpy(s, capeinfo.partno, 16);
193 fprintf(stdout, "%-16s: %s\n", "PART_NUMBER", s);
195 memset(s, '\0', 33);
196 memcpy(s, capeinfo.serial, 12);
197 fprintf(stdout, "%-16s: %s\n", "SERIAL", s);
199 return 0;
203 board_info(int fd, i2c_addr_t address, int flags,
204 enum device_types device_type)
206 int r;
207 uint8_t magic_number[6];
209 r = eeprom_read(fd, address, 0x0000, &magic_number, 6, flags,
210 device_type);
211 if (r == -1) {
212 printf("%-16s: %s\n", "BOARD_NAME", "UNKNOWN");
213 return 0;
216 /* Check for BeagleBone/BeagleBone Black/Cape Magic Number */
217 if (magic_number[0] == 0xaa && magic_number[1] == 0x55 &&
218 magic_number[2] == 0x33 && magic_number[3] == 0xee) {
220 /* Check if Cape Rev A0, Cape Rev A1, or on-board EEPROM */
221 if (magic_number[4] == 'A' && magic_number[5] == '0') {
222 board_info_cape_a0(fd, address, flags, device_type);
223 } else if (magic_number[4] == 'A' && magic_number[5] == '1') {
224 board_info_cape_a1(fd, address, flags, device_type);
225 } else {
226 board_info_beaglebone(fd, address, flags, device_type);
228 } else {
229 printf("%-16s: %s\n", "BOARD_NAME", "UNKNOWN");
232 return 0;