/
feature-onie-syseeprom-command.patch
2082 lines (2077 loc) · 59.6 KB
/
feature-onie-syseeprom-command.patch
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
Add to support onie-syseeprom command.
Copyright (C) 2013,2018 Curt Brune <curt@cumulusnetworks.com>
Copyright (C) 2014,2016,2017 david_yang <david_yang@accton.com>
Copyright (C) 2015 Ellen Wang <ellen@cumulusnetworks.com>
SPDX-License-Identifier: GPL-2.0
diff --git a/include/24cXX.h b/include/24cXX.h
new file mode 100644
index 0000000..5fb4641
--- /dev/null
+++ b/include/24cXX.h
@@ -0,0 +1,66 @@
+/***************************************************************************
+ copyright : (C) by 2002-2003 Stefano Barbato
+ email : stefano@codesink.org
+
+ $Id: 24cXX.h 6048 2012-04-26 10:10:22Z khali $
+ ***************************************************************************/
+
+/***************************************************************************
+ * *
+ * This program is free software; you can redistribute it and/or modify *
+ * it under the terms of the GNU General Public License as published by *
+ * the Free Software Foundation; either version 2 of the License, or *
+ * (at your option) any later version. *
+ * *
+ ***************************************************************************/
+#ifndef _24CXX_H_
+#define _24CXX_H_
+#include <linux/types.h>
+
+#define EEPROM_TYPE_UNKNOWN 0
+#define EEPROM_TYPE_8BIT_ADDR 1
+#define EEPROM_TYPE_16BIT_ADDR 2
+
+/*
+ * SYS_EEPROM_I2C_WRITE_1B_DELAY: delay number of microseconds after writing 1 byte
+ * SYS_EEPROM_I2C_WRITE_2B_DELAY: delay number of microseconds after writing 2 bytes
+ * SYS_EEPROM_I2C_WRITE_3B_DELAY: delay number of microseconds after writing 3 bytes
+ */
+#define SYS_EEPROM_I2C_WRITE_1B_DELAY CONFIG_SYS_EEPROM_I2C_WRITE_1B_DELAY
+#define SYS_EEPROM_I2C_WRITE_2B_DELAY CONFIG_SYS_EEPROM_I2C_WRITE_2B_DELAY
+#define SYS_EEPROM_I2C_WRITE_3B_DELAY CONFIG_SYS_EEPROM_I2C_WRITE_3B_DELAY
+
+struct eeprom
+{
+ char *dev; // device file i.e. /dev/i2c-N
+ int addr; // i2c address
+ int fd; // file descriptor
+ int type; // eeprom type
+};
+
+/*
+ * opens the eeprom device at [dev_fqn] (i.e. /dev/i2c-N) whose address is
+ * [addr] and set the eeprom_24c32 [e]
+ */
+int eeprom_open(char *dev_fqn, int addr, int type, struct eeprom*);
+/*
+ * closees the eeprom device [e]
+ */
+int eeprom_close(struct eeprom *e);
+/*
+ * read and returns the eeprom byte at memory address [mem_addr]
+ * Note: eeprom must have been selected by ioctl(fd,I2C_SLAVE,address)
+ */
+int eeprom_read_byte(struct eeprom* e, __u16 mem_addr);
+/*
+ * read the current byte
+ * Note: eeprom must have been selected by ioctl(fd,I2C_SLAVE,address)
+ */
+int eeprom_read_current_byte(struct eeprom *e);
+/*
+ * writes [data] at memory address [mem_addr]
+ * Note: eeprom must have been selected by ioctl(fd,I2C_SLAVE,address)
+ */
+int eeprom_write_byte(struct eeprom *e, __u16 mem_addr, __u8 data);
+
+#endif
diff --git a/include/applets.src.h b/include/applets.src.h
index b9df5a1..71b8cbd 100644
--- a/include/applets.src.h
+++ b/include/applets.src.h
@@ -344,6 +344,7 @@ IF_WC(APPLET(wc, BB_DIR_USR_BIN, BB_SUID_DROP))
IF_WHOAMI(APPLET_NOFORK(whoami, whoami, BB_DIR_USR_BIN, BB_SUID_DROP, whoami))
IF_YES(APPLET_NOFORK(yes, yes, BB_DIR_USR_BIN, BB_SUID_DROP, yes))
IF_ZCIP(APPLET(zcip, BB_DIR_SBIN, BB_SUID_DROP))
+IF_SYS_EEPROM(APPLET_ODDNAME(onie-syseeprom, onie_syseeprom, BB_DIR_USR_BIN, BB_SUID_DROP, onie_syseeprom))
#if !defined(PROTOTYPES) && !defined(NAME_MAIN) && !defined(MAKE_USAGE) \
&& !defined(MAKE_LINKS) && !defined(MAKE_SUID)
diff --git a/include/i2c-dev.h b/include/i2c-dev.h
new file mode 100644
index 0000000..23f7c2c
--- /dev/null
+++ b/include/i2c-dev.h
@@ -0,0 +1,330 @@
+/*
+ i2c-dev.h - i2c-bus driver, char device interface
+
+ Copyright (C) 1995-97 Simon G. Vogl
+ Copyright (C) 1998-99 Frodo Looijaard <frodol@dds.nl>
+
+ This program is free software; you can redistribute it and/or modify
+ it under the terms of the GNU General Public License as published by
+ the Free Software Foundation; either version 2 of the License, or
+ (at your option) any later version.
+
+ This program is distributed in the hope that it will be useful,
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ GNU General Public License for more details.
+
+ You should have received a copy of the GNU General Public License
+ along with this program; if not, write to the Free Software
+ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
+ MA 02110-1301 USA.
+*/
+
+#ifndef _LINUX_I2C_DEV_H
+#define _LINUX_I2C_DEV_H
+
+#include <linux/types.h>
+#include <sys/ioctl.h>
+#include <stddef.h>
+
+
+/* -- i2c.h -- */
+
+
+/*
+ * I2C Message - used for pure i2c transaction, also from /dev interface
+ */
+struct i2c_msg {
+ __u16 addr; /* slave address */
+ unsigned short flags;
+#define I2C_M_TEN 0x10 /* we have a ten bit chip address */
+#define I2C_M_RD 0x01
+#define I2C_M_NOSTART 0x4000
+#define I2C_M_REV_DIR_ADDR 0x2000
+#define I2C_M_IGNORE_NAK 0x1000
+#define I2C_M_NO_RD_ACK 0x0800
+ short len; /* msg length */
+ char *buf; /* pointer to msg data */
+};
+
+/* To determine what functionality is present */
+
+#define I2C_FUNC_I2C 0x00000001
+#define I2C_FUNC_10BIT_ADDR 0x00000002
+#define I2C_FUNC_PROTOCOL_MANGLING 0x00000004 /* I2C_M_{REV_DIR_ADDR,NOSTART,..} */
+#define I2C_FUNC_SMBUS_PEC 0x00000008
+#define I2C_FUNC_SMBUS_BLOCK_PROC_CALL 0x00008000 /* SMBus 2.0 */
+#define I2C_FUNC_SMBUS_QUICK 0x00010000
+#define I2C_FUNC_SMBUS_READ_BYTE 0x00020000
+#define I2C_FUNC_SMBUS_WRITE_BYTE 0x00040000
+#define I2C_FUNC_SMBUS_READ_BYTE_DATA 0x00080000
+#define I2C_FUNC_SMBUS_WRITE_BYTE_DATA 0x00100000
+#define I2C_FUNC_SMBUS_READ_WORD_DATA 0x00200000
+#define I2C_FUNC_SMBUS_WRITE_WORD_DATA 0x00400000
+#define I2C_FUNC_SMBUS_PROC_CALL 0x00800000
+#define I2C_FUNC_SMBUS_READ_BLOCK_DATA 0x01000000
+#define I2C_FUNC_SMBUS_WRITE_BLOCK_DATA 0x02000000
+#define I2C_FUNC_SMBUS_READ_I2C_BLOCK 0x04000000 /* I2C-like block xfer */
+#define I2C_FUNC_SMBUS_WRITE_I2C_BLOCK 0x08000000 /* w/ 1-byte reg. addr. */
+
+#define I2C_FUNC_SMBUS_BYTE (I2C_FUNC_SMBUS_READ_BYTE | \
+ I2C_FUNC_SMBUS_WRITE_BYTE)
+#define I2C_FUNC_SMBUS_BYTE_DATA (I2C_FUNC_SMBUS_READ_BYTE_DATA | \
+ I2C_FUNC_SMBUS_WRITE_BYTE_DATA)
+#define I2C_FUNC_SMBUS_WORD_DATA (I2C_FUNC_SMBUS_READ_WORD_DATA | \
+ I2C_FUNC_SMBUS_WRITE_WORD_DATA)
+#define I2C_FUNC_SMBUS_BLOCK_DATA (I2C_FUNC_SMBUS_READ_BLOCK_DATA | \
+ I2C_FUNC_SMBUS_WRITE_BLOCK_DATA)
+#define I2C_FUNC_SMBUS_I2C_BLOCK (I2C_FUNC_SMBUS_READ_I2C_BLOCK | \
+ I2C_FUNC_SMBUS_WRITE_I2C_BLOCK)
+
+/* Old name, for compatibility */
+#define I2C_FUNC_SMBUS_HWPEC_CALC I2C_FUNC_SMBUS_PEC
+
+/*
+ * Data for SMBus Messages
+ */
+#define I2C_SMBUS_BLOCK_MAX 32 /* As specified in SMBus standard */
+#define I2C_SMBUS_I2C_BLOCK_MAX 32 /* Not specified but we use same structure */
+union i2c_smbus_data {
+ __u8 byte;
+ __u16 word;
+ __u8 block[I2C_SMBUS_BLOCK_MAX + 2]; /* block[0] is used for length */
+ /* and one more for PEC */
+};
+
+/* smbus_access read or write markers */
+#define I2C_SMBUS_READ 1
+#define I2C_SMBUS_WRITE 0
+
+/* SMBus transaction types (size parameter in the above functions)
+ Note: these no longer correspond to the (arbitrary) PIIX4 internal codes! */
+#define I2C_SMBUS_QUICK 0
+#define I2C_SMBUS_BYTE 1
+#define I2C_SMBUS_BYTE_DATA 2
+#define I2C_SMBUS_WORD_DATA 3
+#define I2C_SMBUS_PROC_CALL 4
+#define I2C_SMBUS_BLOCK_DATA 5
+#define I2C_SMBUS_I2C_BLOCK_BROKEN 6
+#define I2C_SMBUS_BLOCK_PROC_CALL 7 /* SMBus 2.0 */
+#define I2C_SMBUS_I2C_BLOCK_DATA 8
+
+
+/* /dev/i2c-X ioctl commands. The ioctl's parameter is always an
+ * unsigned long, except for:
+ * - I2C_FUNCS, takes pointer to an unsigned long
+ * - I2C_RDWR, takes pointer to struct i2c_rdwr_ioctl_data
+ * - I2C_SMBUS, takes pointer to struct i2c_smbus_ioctl_data
+ */
+#define I2C_RETRIES 0x0701 /* number of times a device address should
+ be polled when not acknowledging */
+#define I2C_TIMEOUT 0x0702 /* set timeout in units of 10 ms */
+
+/* NOTE: Slave address is 7 or 10 bits, but 10-bit addresses
+ * are NOT supported! (due to code brokenness)
+ */
+#define I2C_SLAVE 0x0703 /* Use this slave address */
+#define I2C_SLAVE_FORCE 0x0706 /* Use this slave address, even if it
+ is already in use by a driver! */
+#define I2C_TENBIT 0x0704 /* 0 for 7 bit addrs, != 0 for 10 bit */
+
+#define I2C_FUNCS 0x0705 /* Get the adapter functionality mask */
+
+#define I2C_RDWR 0x0707 /* Combined R/W transfer (one STOP only) */
+
+#define I2C_PEC 0x0708 /* != 0 to use PEC with SMBus */
+#define I2C_SMBUS 0x0720 /* SMBus transfer */
+
+
+/* This is the structure as used in the I2C_SMBUS ioctl call */
+struct i2c_smbus_ioctl_data {
+ __u8 read_write;
+ __u8 command;
+ __u32 size;
+ union i2c_smbus_data *data;
+};
+
+/* This is the structure as used in the I2C_RDWR ioctl call */
+struct i2c_rdwr_ioctl_data {
+ struct i2c_msg *msgs; /* pointers to i2c_msgs */
+ __u32 nmsgs; /* number of i2c_msgs */
+};
+
+#define I2C_RDRW_IOCTL_MAX_MSGS 42
+
+
+static inline __s32 i2c_smbus_access(int file, char read_write, __u8 command,
+ int size, union i2c_smbus_data *data)
+{
+ struct i2c_smbus_ioctl_data args;
+
+ args.read_write = read_write;
+ args.command = command;
+ args.size = size;
+ args.data = data;
+ return ioctl(file,I2C_SMBUS,&args);
+}
+
+
+static inline __s32 i2c_smbus_write_quick(int file, __u8 value)
+{
+ return i2c_smbus_access(file,value,0,I2C_SMBUS_QUICK,NULL);
+}
+
+static inline __s32 i2c_smbus_read_byte(int file)
+{
+ union i2c_smbus_data data;
+ if (i2c_smbus_access(file,I2C_SMBUS_READ,0,I2C_SMBUS_BYTE,&data))
+ return -1;
+ else
+ return 0x0FF & data.byte;
+}
+
+static inline __s32 i2c_smbus_write_byte(int file, __u8 value)
+{
+ return i2c_smbus_access(file,I2C_SMBUS_WRITE,value,
+ I2C_SMBUS_BYTE,NULL);
+}
+
+static inline __s32 i2c_smbus_read_byte_data(int file, __u8 command)
+{
+ union i2c_smbus_data data;
+ if (i2c_smbus_access(file,I2C_SMBUS_READ,command,
+ I2C_SMBUS_BYTE_DATA,&data))
+ return -1;
+ else
+ return 0x0FF & data.byte;
+}
+
+static inline __s32 i2c_smbus_write_byte_data(int file, __u8 command,
+ __u8 value)
+{
+ union i2c_smbus_data data;
+ data.byte = value;
+ return i2c_smbus_access(file,I2C_SMBUS_WRITE,command,
+ I2C_SMBUS_BYTE_DATA, &data);
+}
+
+static inline __s32 i2c_smbus_read_word_data(int file, __u8 command)
+{
+ union i2c_smbus_data data;
+ if (i2c_smbus_access(file,I2C_SMBUS_READ,command,
+ I2C_SMBUS_WORD_DATA,&data))
+ return -1;
+ else
+ return 0x0FFFF & data.word;
+}
+
+static inline __s32 i2c_smbus_write_word_data(int file, __u8 command,
+ __u16 value)
+{
+ union i2c_smbus_data data;
+ data.word = value;
+ return i2c_smbus_access(file,I2C_SMBUS_WRITE,command,
+ I2C_SMBUS_WORD_DATA, &data);
+}
+
+static inline __s32 i2c_smbus_process_call(int file, __u8 command, __u16 value)
+{
+ union i2c_smbus_data data;
+ data.word = value;
+ if (i2c_smbus_access(file,I2C_SMBUS_WRITE,command,
+ I2C_SMBUS_PROC_CALL,&data))
+ return -1;
+ else
+ return 0x0FFFF & data.word;
+}
+
+
+/* Returns the number of read bytes */
+static inline __s32 i2c_smbus_read_block_data(int file, __u8 command,
+ __u8 *values)
+{
+ union i2c_smbus_data data;
+ int i;
+ if (i2c_smbus_access(file,I2C_SMBUS_READ,command,
+ I2C_SMBUS_BLOCK_DATA,&data))
+ return -1;
+ else {
+ for (i = 1; i <= data.block[0]; i++)
+ values[i-1] = data.block[i];
+ return data.block[0];
+ }
+}
+
+static inline __s32 i2c_smbus_write_block_data(int file, __u8 command,
+ __u8 length, const __u8 *values)
+{
+ union i2c_smbus_data data;
+ int i;
+ if (length > 32)
+ length = 32;
+ for (i = 1; i <= length; i++)
+ data.block[i] = values[i-1];
+ data.block[0] = length;
+ return i2c_smbus_access(file,I2C_SMBUS_WRITE,command,
+ I2C_SMBUS_BLOCK_DATA, &data);
+}
+
+/* Returns the number of read bytes */
+/* Until kernel 2.6.22, the length is hardcoded to 32 bytes. If you
+ ask for less than 32 bytes, your code will only work with kernels
+ 2.6.23 and later. */
+static inline __s32 i2c_smbus_read_i2c_block_data(int file, __u8 command,
+ __u8 length, __u8 *values)
+{
+ union i2c_smbus_data data;
+ int i;
+
+ if (length > 32)
+ length = 32;
+ data.block[0] = length;
+ if (i2c_smbus_access(file,I2C_SMBUS_READ,command,
+ length == 32 ? I2C_SMBUS_I2C_BLOCK_BROKEN :
+ I2C_SMBUS_I2C_BLOCK_DATA,&data))
+ return -1;
+ else {
+ for (i = 1; i <= data.block[0]; i++)
+ values[i-1] = data.block[i];
+ return data.block[0];
+ }
+}
+
+static inline __s32 i2c_smbus_write_i2c_block_data(int file, __u8 command,
+ __u8 length,
+ const __u8 *values)
+{
+ union i2c_smbus_data data;
+ int i;
+ if (length > 32)
+ length = 32;
+ for (i = 1; i <= length; i++)
+ data.block[i] = values[i-1];
+ data.block[0] = length;
+ return i2c_smbus_access(file,I2C_SMBUS_WRITE,command,
+ I2C_SMBUS_I2C_BLOCK_BROKEN, &data);
+}
+
+/* Returns the number of read bytes */
+static inline __s32 i2c_smbus_block_process_call(int file, __u8 command,
+ __u8 length, __u8 *values)
+{
+ union i2c_smbus_data data;
+ int i;
+ if (length > 32)
+ length = 32;
+ for (i = 1; i <= length; i++)
+ data.block[i] = values[i-1];
+ data.block[0] = length;
+ if (i2c_smbus_access(file,I2C_SMBUS_WRITE,command,
+ I2C_SMBUS_BLOCK_PROC_CALL,&data))
+ return -1;
+ else {
+ for (i = 1; i <= data.block[0]; i++)
+ values[i-1] = data.block[i];
+ return data.block[0];
+ }
+}
+
+
+#endif /* _LINUX_I2C_DEV_H */
diff --git a/include/onie_tlvinfo.h b/include/onie_tlvinfo.h
new file mode 100644
index 0000000..23ec9c5
--- /dev/null
+++ b/include/onie_tlvinfo.h
@@ -0,0 +1,173 @@
+/*
+ * The Definition of the TlvInfo EEPROM format can be found at onie.org or
+ * github.com/onie
+ */
+#include "libbb.h"
+#include <asm/byteorder.h>
+
+#define be16_to_cpu(x) __be16_to_cpu(x)
+#define cpu_to_be16(x) __cpu_to_be16(x)
+
+#ifndef FALSE
+#define FALSE 0
+#endif
+
+#ifndef TRUE
+#define TRUE (!FALSE)
+#endif
+
+//typedef unsigned char bool;
+
+#define min(x, y) ((x) > (y) ? (y) : (x))
+
+/*
+ * Tlvinf header: Layout of the header for the TlvInfo format
+ *
+ * See the end of this file for details of this eeprom format
+ */
+struct __attribute__ ((__packed__)) tlvinfo_header_s {
+ char signature[8]; /* 0x00 - 0x07 EEPROM Tag "TlvInfo" */
+ u_int8_t version; /* 0x08 Structure version */
+ u_int16_t totallen; /* 0x09 - 0x0A Length of all data which follows */
+};
+typedef struct tlvinfo_header_s tlvinfo_header_t;
+
+// Header Field Constants
+#define TLV_INFO_ID_STRING "TlvInfo"
+#define TLV_INFO_VERSION 0x01
+#define TLV_TOTAL_LEN_MAX (SYS_EEPROM_SIZE - sizeof(tlvinfo_header_t))
+
+/*
+ * TlvInfo TLV: Layout of a TLV field
+ */
+struct __attribute__ ((__packed__)) tlvinfo_tlv_s {
+ u_int8_t type;
+ u_int8_t length;
+ u_int8_t value[0];
+};
+typedef struct tlvinfo_tlv_s tlvinfo_tlv_t;
+
+/* Maximum length of a TLV value in bytes */
+#define TLV_VALUE_MAX_LEN 255
+
+/**
+ * The TLV Types.
+ *
+ * Keep these in sync with tlv_code_list in cmd_sys_eeprom.c
+ */
+#define TLV_CODE_PRODUCT_NAME 0x21
+#define TLV_CODE_PART_NUMBER 0x22
+#define TLV_CODE_SERIAL_NUMBER 0x23
+#define TLV_CODE_MAC_BASE 0x24
+#define TLV_CODE_MANUF_DATE 0x25
+#define TLV_CODE_DEVICE_VERSION 0x26
+#define TLV_CODE_LABEL_REVISION 0x27
+#define TLV_CODE_PLATFORM_NAME 0x28
+#define TLV_CODE_ONIE_VERSION 0x29
+#define TLV_CODE_MAC_SIZE 0x2A
+#define TLV_CODE_MANUF_NAME 0x2B
+#define TLV_CODE_MANUF_COUNTRY 0x2C
+#define TLV_CODE_VENDOR_NAME 0x2D
+#define TLV_CODE_DIAG_VERSION 0x2E
+#define TLV_CODE_SERVICE_TAG 0x2F
+#define TLV_CODE_VENDOR_EXT 0xFD
+#define TLV_CODE_CRC_32 0xFE
+
+/*
+ * Struct for displaying the TLV codes and names.
+ */
+struct tlv_code_desc {
+ u_int8_t m_code;
+ char* m_name;
+};
+
+/*
+ * List of TLV codes and names.
+ */
+static const struct tlv_code_desc tlv_code_list[] = {
+ { TLV_CODE_PRODUCT_NAME , "Product Name"},
+ { TLV_CODE_PART_NUMBER , "Part Number"},
+ { TLV_CODE_SERIAL_NUMBER , "Serial Number"},
+ { TLV_CODE_MAC_BASE , "Base MAC Address"},
+ { TLV_CODE_MANUF_DATE , "Manufacture Date"},
+ { TLV_CODE_DEVICE_VERSION , "Device Version"},
+ { TLV_CODE_LABEL_REVISION , "Label Revision"},
+ { TLV_CODE_PLATFORM_NAME , "Platform Name"},
+ { TLV_CODE_ONIE_VERSION , "ONIE Version"},
+ { TLV_CODE_MAC_SIZE , "MAC Addresses"},
+ { TLV_CODE_MANUF_NAME , "Manufacturer"},
+ { TLV_CODE_MANUF_COUNTRY , "Country Code"},
+ { TLV_CODE_VENDOR_NAME , "Vendor Name"},
+ { TLV_CODE_DIAG_VERSION , "Diag Version"},
+ { TLV_CODE_SERVICE_TAG , "Service Tag"},
+ { TLV_CODE_VENDOR_EXT , "Vendor Extension"},
+ { TLV_CODE_CRC_32 , "CRC-32"},
+};
+
+static inline const char* tlv_type2name(u_int8_t type)
+{
+ char* name = "Unknown";
+ int i;
+
+ for (i = 0; i < sizeof(tlv_code_list)/sizeof(tlv_code_list[0]); i++) {
+ if (tlv_code_list[i].m_code == type) {
+ name = tlv_code_list[i].m_name;
+ break;
+ }
+ }
+ return name;
+}
+
+/*
+ * The max decode value is currently for the 'raw' type or the 'vendor
+ * extension' type, both of which have the same decode format. The
+ * max decode string size is computed as follows:
+ *
+ * strlen(" 0xFF") * TLV_VALUE_MAX_LEN + 1
+ *
+ */
+#define TLV_DECODE_VALUE_MAX_LEN ((5 * TLV_VALUE_MAX_LEN) + 1)
+
+
+/*
+ * SYS_EEPROM_SIZE: size of usable eeprom
+ * SYS_EEPROM_I2C_DEVICE: i2c-bus
+ * SYS_EEPROM_I2C_ADDR: address on the bus
+ * SYS_EEPROM_I2C_MEM_ADDR_BITS: number of bits to address a byte in eeprom
+ * SYS_EEPROM_MTD_DEVICE: MTD device name of the eeprom
+ * SYS_EEPROM_MAX_SIZE: Total size of the eeprom
+ * SYS_EEPROM_OFFSET: offset from where the ONIE header starts
+ */
+#define SYS_EEPROM_SIZE CONFIG_SYS_EEPROM_SIZE
+#define SYS_EEPROM_I2C_DEVICE CONFIG_SYS_EEPROM_I2C_DEVICE
+#define SYS_EEPROM_I2C_ADDR CONFIG_SYS_EEPROM_I2C_ADDR
+#define SYS_EEPROM_I2C_MEM_ADDR_BITS CONFIG_SYS_EEPROM_I2C_MEM_ADDR_BITS
+#define SYS_EEPROM_MTD_DEVICE CONFIG_SYS_EEPROM_MTD_DEVICE
+#define SYS_EEPROM_MAX_SIZE CONFIG_SYS_EEPROM_MAX_SIZE
+#define SYS_EEPROM_OFFSET CONFIG_SYS_EEPROM_OFFSET
+
+#if CONFIG_SYS_EEPROM_SIZE == 0
+ #error CONFIG_SYS_EEPROM_SIZE not specified!
+#endif
+
+#ifdef CONFIG_SYS_EEPROM_DEVICE_I2C
+ #if CONFIG_SYS_EEPROM_I2C_ADDR == 0
+ #error CONFIG_SYS_EEPROM_I2C_ADDR not specified!
+ #endif
+#endif
+
+#if (SYS_EEPROM_SIZE + SYS_EEPROM_OFFSET > SYS_EEPROM_MAX_SIZE)
+ #error SYS_EEPROM_SIZE + SYS_EEPROM_OFFSET is greater than SYS_EEPROM_MAX_SIZE
+#endif
+
+// Access functions to onie_tlvinfo
+void show_eeprom(u_int8_t *eeprom);
+int read_eeprom(u_int8_t *eeprom);
+int prog_eeprom(u_int8_t * eeprom);
+void update_eeprom_header(u_int8_t *eeprom);
+bool tlvinfo_multiple_tcode_allowed(int tcode);
+bool tlvinfo_find_tlv(u_int8_t *eeprom, u_int8_t tcode, int *eeprom_index);
+bool tlvinfo_delete_tlv(u_int8_t * eeprom, u_int8_t code);
+bool tlvinfo_add_tlv(u_int8_t * eeprom, int tcode, char * strval);
+bool tlvinfo_decode_tlv(u_int8_t *eeprom, u_int8_t tcode, char* value);
+void show_tlv_code_list(void);
diff --git a/include/sys_eeprom.h b/include/sys_eeprom.h
new file mode 100644
index 0000000..94dfd5a
--- /dev/null
+++ b/include/sys_eeprom.h
@@ -0,0 +1,2 @@
+int read_sys_eeprom(void *eeprom_data, int offset, int len);
+int write_sys_eeprom(void *eeprom_data, int len);
diff --git a/miscutils/24cXX.c b/miscutils/24cXX.c
new file mode 100644
index 0000000..ccc0965
--- /dev/null
+++ b/miscutils/24cXX.c
@@ -0,0 +1,182 @@
+/***************************************************************************
+ copyright : (C) by 2002-2003 Stefano Barbato
+ email : stefano@codesink.org
+
+ $Id: 24cXX.c 6228 2014-02-20 08:37:15Z khali $
+ ***************************************************************************/
+
+/***************************************************************************
+ * *
+ * This program is free software; you can redistribute it and/or modify *
+ * it under the terms of the GNU General Public License as published by *
+ * the Free Software Foundation; either version 2 of the License, or *
+ * (at your option) any later version. *
+ * *
+ ***************************************************************************/
+#include <stdio.h>
+#include <fcntl.h>
+#include <unistd.h>
+#include <stdlib.h>
+#include <linux/fs.h>
+#include "i2c-dev.h"
+#include <sys/types.h>
+#include <sys/ioctl.h>
+#include <errno.h>
+#include <assert.h>
+#include <string.h>
+#include "24cXX.h"
+
+static int i2c_write_1b(struct eeprom *e, __u8 buf)
+{
+ int r;
+ // we must simulate a plain I2C byte write with SMBus functions
+ r = i2c_smbus_write_byte(e->fd, buf);
+ if(r < 0)
+ fprintf(stderr, "Error i2c_write_1b: %s\n", strerror(errno));
+ usleep(SYS_EEPROM_I2C_WRITE_1B_DELAY);
+ return r;
+}
+
+static int i2c_write_2b(struct eeprom *e, __u8 buf[2])
+{
+ int r;
+ // we must simulate a plain I2C byte write with SMBus functions
+ r = i2c_smbus_write_byte_data(e->fd, buf[0], buf[1]);
+ if(r < 0)
+ fprintf(stderr, "Error i2c_write_2b: %s\n", strerror(errno));
+ usleep(SYS_EEPROM_I2C_WRITE_2B_DELAY);
+ return r;
+}
+
+static int i2c_write_3b(struct eeprom *e, __u8 buf[3])
+{
+ int r;
+ // we must simulate a plain I2C byte write with SMBus functions
+ // the __u16 data field will be byte swapped by the SMBus protocol
+ r = i2c_smbus_write_word_data(e->fd, buf[0], buf[2] << 8 | buf[1]);
+ if(r < 0)
+ fprintf(stderr, "Error i2c_write_3b: %s\n", strerror(errno));
+ usleep(SYS_EEPROM_I2C_WRITE_3B_DELAY);
+ return r;
+}
+
+
+#define CHECK_I2C_FUNC( var, label ) \
+ do { if(0 == (var & label)) { \
+ fprintf(stderr, "\nError: " \
+ #label " function is required. Program halted.\n\n"); \
+ exit(1); } \
+ } while(0);
+
+int eeprom_open(char *dev_fqn, int addr, int type, struct eeprom* e)
+{
+ int fd, r;
+ unsigned long funcs;
+ e->fd = e->addr = 0;
+ e->dev = 0;
+
+ fd = open(dev_fqn, O_RDWR);
+ if(fd <= 0)
+ return -1;
+
+ // get funcs list
+ if((r = ioctl(fd, I2C_FUNCS, &funcs) < 0))
+ return r;
+
+
+ // check for req funcs
+ CHECK_I2C_FUNC( funcs, I2C_FUNC_SMBUS_READ_BYTE );
+ CHECK_I2C_FUNC( funcs, I2C_FUNC_SMBUS_WRITE_BYTE );
+ CHECK_I2C_FUNC( funcs, I2C_FUNC_SMBUS_READ_BYTE_DATA );
+ CHECK_I2C_FUNC( funcs, I2C_FUNC_SMBUS_WRITE_BYTE_DATA );
+ CHECK_I2C_FUNC( funcs, I2C_FUNC_SMBUS_READ_WORD_DATA );
+ CHECK_I2C_FUNC( funcs, I2C_FUNC_SMBUS_WRITE_WORD_DATA );
+
+ // set working device
+ if( ( r = ioctl(fd, I2C_SLAVE, addr)) < 0)
+ return r;
+ e->fd = fd;
+ e->addr = addr;
+ e->dev = dev_fqn;
+ e->type = type;
+ return 0;
+}
+
+int eeprom_close(struct eeprom *e)
+{
+ close(e->fd);
+ e->fd = -1;
+ e->dev = 0;
+ e->type = EEPROM_TYPE_UNKNOWN;
+ return 0;
+}
+
+#if 0
+int eeprom_24c32_write_byte(struct eeprom *e, __u16 mem_addr, __u8 data)
+{
+ __u8 buf[3] = { (mem_addr >> 8) & 0x00ff, mem_addr & 0x00ff, data };
+ return i2c_write_3b(e, buf);
+}
+
+
+int eeprom_24c32_read_current_byte(struct eeprom* e)
+{
+ ioctl(e->fd, BLKFLSBUF); // clear kernel read buffer
+ return i2c_smbus_read_byte(e->fd);
+}
+
+int eeprom_24c32_read_byte(struct eeprom* e, __u16 mem_addr)
+{
+ int r;
+ ioctl(e->fd, BLKFLSBUF); // clear kernel read buffer
+ __u8 buf[2] = { (mem_addr >> 8) & 0x0ff, mem_addr & 0x0ff };
+ r = i2c_write_2b(e, buf);
+ if (r < 0)
+ return r;
+ r = i2c_smbus_read_byte(e->fd);
+ return r;
+}
+#endif
+
+
+int eeprom_read_current_byte(struct eeprom* e)
+{
+ ioctl(e->fd, BLKFLSBUF); // clear kernel read buffer
+ return i2c_smbus_read_byte(e->fd);
+}
+
+int eeprom_read_byte(struct eeprom* e, __u16 mem_addr)
+{
+ int r;
+ ioctl(e->fd, BLKFLSBUF); // clear kernel read buffer
+ if(e->type == EEPROM_TYPE_8BIT_ADDR)
+ {
+ __u8 buf = mem_addr & 0x0ff;
+ r = i2c_write_1b(e, buf);
+ } else if(e->type == EEPROM_TYPE_16BIT_ADDR) {
+ __u8 buf[2] = { (mem_addr >> 8) & 0x0ff, mem_addr & 0x0ff };
+ r = i2c_write_2b(e, buf);
+ } else {
+ fprintf(stderr, "ERR: unknown eeprom type\n");
+ return -1;
+ }
+ if (r < 0)
+ return r;
+ r = i2c_smbus_read_byte(e->fd);
+ return r;
+}
+
+int eeprom_write_byte(struct eeprom *e, __u16 mem_addr, __u8 data)
+{
+ if(e->type == EEPROM_TYPE_8BIT_ADDR) {
+ __u8 buf[2] = { mem_addr & 0x00ff, data };
+ return i2c_write_2b(e, buf);
+ } else if(e->type == EEPROM_TYPE_16BIT_ADDR) {
+ __u8 buf[3] =
+ { (mem_addr >> 8) & 0x00ff, mem_addr & 0x00ff, data };
+ return i2c_write_3b(e, buf);
+ } else {
+ fprintf(stderr, "ERR: unknown eeprom type\n");
+ return -1;
+ }
+}
diff --git a/miscutils/Config.src b/miscutils/Config.src
index 4d6425f..cdae45b 100644
--- a/miscutils/Config.src
+++ b/miscutils/Config.src
@@ -546,4 +546,108 @@ config WATCHDOG
certain amount of time, the watchdog device assumes the system has
hung, and will cause the hardware to reboot.
+config SYS_EEPROM
+ bool "ONIE system EEPROM"
+ default n
+ help
+ Display and program the system EEPROM data block.
+ The onie-syseeprom tool is included.
+
+menu "ONIE system EEPROM config"
+ depends on SYS_EEPROM
+
+choice
+ prompt "Choose which device type of EEPROM"
+ depends on SYS_EEPROM
+ help
+ Choose which device type of EEPROM.
+
+config SYS_EEPROM_DEVICE_I2C
+ bool "i2c"
+
+config SYS_EEPROM_DEVICE_MTD
+ bool "MTD"
+
+endchoice
+
+config SYS_EEPROM_I2C_DEVICE
+ string "i2c bus"
+ default "/dev/i2c-1"
+ depends on SYS_EEPROM_DEVICE_I2C
+ help
+ i2c bus name of the eeprom. e.g., /dev/i2c-1.
+
+config SYS_EEPROM_I2C_ADDR
+ hex "device address on the i2c bus"
+ default 0
+ depends on SYS_EEPROM_DEVICE_I2C
+ help
+ i2c device address of the eeprom. e.g., 0x57.
+
+config SYS_EEPROM_I2C_MEM_ADDR_BITS
+ int "eeprom address bits"
+ range 8 16
+ default 8
+ depends on SYS_EEPROM_DEVICE_I2C
+ help
+ Number of bits needed to address a byte in the EEPROM.
+ e.g., 8 or 16.
+
+config SYS_EEPROM_I2C_WRITE_1B_DELAY
+ int "delay time for i2c writing 1 byte"
+ range 10 1000000
+ default 10
+ depends on SYS_EEPROM_DEVICE_I2C
+ help
+ Delay number of microseconds after writing 1 byte.
+
+config SYS_EEPROM_I2C_WRITE_2B_DELAY
+ int "delay time for i2c writing 2 bytes"
+ range 10 1000000
+ default 1000
+ depends on SYS_EEPROM_DEVICE_I2C
+ help
+ Delay number of microseconds after writing 2 bytes.
+
+config SYS_EEPROM_I2C_WRITE_3B_DELAY
+ int "delay time for i2c writing 3 bytes"
+ range 10 1000000
+ default 10
+ depends on SYS_EEPROM_DEVICE_I2C
+ help
+ Delay number of microseconds after writing 3 bytes.
+
+config SYS_EEPROM_MTD_DEVICE
+ string "MTD device"
+ default "/dev/mtd-hw-info"
+ depends on SYS_EEPROM_DEVICE_MTD
+ help
+ MTD device name of the eeprom. e.g., /dev/mtd-hw-info.
+
+config SYS_EEPROM_OFFSET
+ int "offset"
+ range 0 65536
+ default 0
+ depends on SYS_EEPROM
+ help
+ Offset from where the ONIE header starts.
+
+config SYS_EEPROM_SIZE
+ int "usable eeprom size in byte"
+ range 0 2048
+ default 0
+ depends on SYS_EEPROM
+ help
+ Size of usable eeprom.
+
+config SYS_EEPROM_MAX_SIZE
+ int "total size in byte"
+ range 256 65536
+ default 2048
+ depends on SYS_EEPROM
+ help
+ Total size of the eeprom.
+
+endmenu
+
endmenu
diff --git a/miscutils/Kbuild.src b/miscutils/Kbuild.src
index 9177818..a78273d 100644
--- a/miscutils/Kbuild.src
+++ b/miscutils/Kbuild.src
@@ -51,3 +51,6 @@ lib-$(CONFIG_TTYSIZE) += ttysize.o
lib-$(CONFIG_UBOOT_ENV) += fw_env.o
lib-$(CONFIG_VOLNAME) += volname.o
lib-$(CONFIG_WATCHDOG) += watchdog.o
+lib-$(CONFIG_SYS_EEPROM) += sys_eeprom.o onie_tlvinfo.o
+lib-$(CONFIG_SYS_EEPROM_DEVICE_I2C) += sys_eeprom_i2c.o 24cXX.o
+lib-$(CONFIG_SYS_EEPROM_DEVICE_MTD) += sys_eeprom_mtd.o
diff --git a/miscutils/onie_tlvinfo.c b/miscutils/onie_tlvinfo.c
new file mode 100644
index 0000000..26b6ec5
--- /dev/null
+++ b/miscutils/onie_tlvinfo.c
@@ -0,0 +1,754 @@
+#include "libbb.h"
+#include "onie_tlvinfo.h"
+#include "sys_eeprom.h"
+
+/* Set to 1 if we've read EEPROM into memory */
+static int has_been_read = 0;
+/* Set to 1 if the EEPROM contents were valid when read from hardware */
+static int hw_eeprom_valid = 1;
+
+unsigned long crc32 (unsigned long crc, const unsigned char *buf, unsigned len)
+{
+ if (!global_crc32_table) {
+ global_crc32_table = crc32_filltable(NULL, 0);
+ }
+ return crc32_block_endian0( crc ^ 0xffffffffL, buf, len, global_crc32_table) ^ 0xffffffffL;
+}
+
+static inline int is_multicast_ether_addr(const u_int8_t *addr)
+{
+ return 0x01 & addr[0];
+}
+
+static inline int is_zero_ether_addr(const u_int8_t *addr)
+{
+ return !(addr[0] | addr[1] | addr[2] | addr[3] | addr[4] | addr[5]);
+}
+
+static inline int is_valid_ether_addr(const u_int8_t *addr)
+{
+ return !is_multicast_ether_addr(addr) && !is_zero_ether_addr(addr);
+}
+
+static int set_bytes(char *buf, const char *string, int * converted_accum)
+{
+ char *p = (char *) string;
+ int i;
+ uint byte;
+
+ if (!p) {
+ printf("ERROR: NULL string passed in.\n");
+ return -1;
+ }
+ /* Convert string to bytes */
+ for (i = 0, p = (char *)string; (i < TLV_VALUE_MAX_LEN) && (*p != 0);
+ i++) {
+ while ((*p == ' ') || (*p == '\t') || (*p == ',') ||
+ (*p == ';')) {
+ p++;
+ }
+ if (*p != 0) {
+ if (!isdigit(*p)) {
+ printf("ERROR: Non-digit found in byte string: (%s)\n", string);
+ return -1;
+ }
+ byte = strtoul(p, &p, 0);
+ if (byte >= 256) {
+ printf("ERROR: The value specified is greater than 255: (%u) " \
+ "in string: %s\n", byte, string);
+ return -1;
+ }
+ buf[i] = byte & 0xFF;
+ }
+ }
+ if ((i == TLV_VALUE_MAX_LEN) && (*p != 0)) {
+ printf("ERROR: Trying to assign too many bytes "
+ "(max: %d) in string: %s\n", TLV_VALUE_MAX_LEN, string);
+ return -1;