Skip to end of metadata
Go to start of metadata

You are viewing an old version of this content. View the current version.

Compare with Current View Version History

Version 1 Next »

Overview

This script writes TLV (Tag-Length-Value) structured data into an I2C EEPROM. It supports a variety of system-specific keys that define metadata for the device.

The script:

  • Builds TLV-formatted payloads.

  • Writes the data into an EEPROM via I2C.

  • Supports custom-defined keys.

  • Calculates and appends a CRC checksum to ensure data integrity.


Usage

python3 TLV_write.py <i2c_bus> <eeprom_address> [--yes] <key> <value> <key> <value> ...

Finding the I2C Bus Number

To determine the correct I2C bus number, run the following command:

sudo i2cdetect -y 1

If no EEPROM device is found, repeat the command with different bus numbers (e.g., 2, 3, 4, etc.) until you detect an address 0x50 or 0x56. Once found, use the corresponding bus number in the script.

Example:

sudo i2cdetect -y 2

If the output shows:

     0  1  2  3  4  5  6  7  8  9  A  B  C  D  E  F
00:          -- -- -- -- -- -- -- -- -- -- -- -- -- 
10: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- 
20: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- 
30: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- 
40: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- 
50: 50 -- -- -- -- -- -- -- -- -- -- -- -- -- -- --

Then bus 2 and address 0x50 should be used in the script.

Arguments

Argument

Description

i2c_bus

The I2C bus number.

eeprom_address

The EEPROM address (hex or decimal).

--yes (optional)

Automatically confirms the operation (skips user confirmation).

<key> <value>

One or more key-value pairs to write to EEPROM.

Example Usage

python3 TLV_write.py 2 0x50 TLV_CODE_SYS_NAME "MySystem" TLV_CODE_SYS_VERSION "1.0"

This writes:

  • System Name: "MySystem"

  • System Version: "1.0" to EEPROM at I2C bus 2, address 0x50.


Supported TLV Keys

Each TLV Key has a Hex Code, Max Length, and belongs to a DMIDecode Type.

System Information (DMIDecode Type: 1)

TLV Key

Hex Code

Max Length (bytes)

TLV_CODE_FAMILY

0x20

20

TLV_CODE_MANUF_DATE

0x23

10

TLV_CODE_PLATFORM_NAME

0x24

20

TLV_CODE_MANUF_NAME

0x25

20

TLV_CODE_VENDOR_NAME

0x27

20

TLV_CODE_SYS_NAME

0x30

20

TLV_CODE_SYS_SKU

0x31

20

TLV_CODE_SYS_SERIAL_NUMBER

0x32

24

TLV_CODE_SYS_VERSION

0x33

5

NIO Information (DMIDecode Type: 2)

TLV Key

Hex Code

Max Length (bytes)

TLV_CODE_NIO_NAME

0x40

20

TLV_CODE_NIO_SERIAL_NUMBER

0x41

24

TLV_CODE_NIO_VERSION

0x42

5

Chassis Information (DMIDecode Type: 3)

TLV Key

Hex Code

Max Length (bytes)

TLV_CODE_CHS_SERIAL_NUMBER

0x50

24

TLV_CODE_CHS_VERSION

0x51

5

TLV_CODE_CONFIG_CODE need to add


EEPROM Writing Process

  1. Validate inputs: Ensure keys exist and values meet length constraints.

  2. Format as TLV: Convert the data into TLV format with type, length, and value.

  3. Append CRC Checksum: Ensure data integrity.

  4. Clear EEPROM: Overwrite existing data with zeros before writing new content.

  5. Write TLV data: Save formatted TLV data to the EEPROM via I2C.


Error Handling

If invalid input is provided, the script will:

  • Print an error message.

  • Exit without modifying EEPROM contents.

Common Errors

Error Message

Cause

Error: Unknown key

Provided TLV key is not in the supported list.

Error: Value for key 'X' is too long

Input exceeds the maximum allowed length.

Error: MAC address must have 6 parts

Incorrect MAC format.

Error: I2C write error

I2C communication issue during EEPROM write.

Error: Total TLV data length exceeds EEPROM capacity

Data is too large for EEPROM storage.


Dependencies

This script requires:

  • Python 3

  • smbus (I2C communication)

  • struct and binascii (binary manipulation)

To install missing dependencies:

sudo apt install python3-smbus

Notes

  • The script assumes EEPROM has a 256-byte capacity.

  • Page size is 16 bytes (writes are done in chunks).

  • The script calculates CRC-32 to ensure data integrity.


  • No labels