Special Configuration Section of the EEPROM
All Tibbo devices have EEPROM (accessible through the stor. object). The bottom 28 bytes of the EEPROM form a so-called "special configuration section."
This section includes:
- 8 bytes for storing the MAC address of the device
- 20 bytes for storing the password of the device
By default, the special configuration area is not accessible to your application — the stor.base property takes care of that. Unless you change it, this property specifies that your application's storage area starts at address 29. To see the MAC or password data, set stor.base to 1. Reminder: EEPROM addresses are counted from 1.
MAC Address
Bytes 1-8 of the EEPROM store the MAC address of the device. On power-up, the MAC address is read out from the EEPROM and programmed into the Ethernet controller. You can always check the current MAC through the net.mac read-only property of the net. object, but there is no direct way to change it. Instead, you can change the MAC address data in the EEPROM itself. Next time the device boots up, it will start using this new address.
The MAC address data in the EEPROM has a special format — you have to follow it if you want the MAC to be recognized by TiOS. Here is this format:
addr1 |
addr2 |
addr3 |
addr4 |
addr5 |
addr6 |
addr7 |
addr8 |
6 |
MAC0 |
MAC1 |
MAC2 |
MAC3 |
MAC4 |
MAC5 |
Checksum |
The byte at address 1 must be set to 6 — this means that 6 bytes of data follow (a MAC address consists of 6 bytes). Addresses from 2 to 7 carry the MAC address itself. Address 8 stores the checksum, which is calculated like this: 255-(modulo8_sum_of_addr_1_through_7).
Here is a sample code that stores a new MAC address into the EEPROM and then reboots the device:
dim s as string
dim x as byte
...
s= "0.2.123.124.220.240" 'supposing, we want to set this MAC
...
...
s=chr(6)+ddval(s) 'added first byte (always 6) and converted readable MAC into bytes
x=255-strsum(s) 'calculated checksum and assigned the result to a BYTE variable ("modulo 8" checksum)
s=s+chr(x) 'now our string is ready
stor.base(1) 'will access the EEPROM from the bottom
x=stor.set(s,1) 'save the data
if x<>len(s) then 'it is a good programming practive to check the result
'failed
else
sys.reboot 'new MAC set, reboot!
end if
...
There are limitations on what MAC address you can set. When loading the MAC into the Ethernet controller, the device always resets the first byte of this address to 0. For example, if you set the MAC to 1.2.3.4.5.6 then the actual MAC used by the device will be 0.2.3.4.5.6.
If you write incorrect MAC data (wrong "length" byte or erroneous checksum), the device will ignore the stored MAC and boot up with the default MAC, which is 0.1.2.3.4.100.
Password
Bytes 9-28 store the device password. The password is stored in the following format:
addr9 |
up to 16 bytes |
3 bytes |
len |
password |
MD5 check data |
The byte at address 9 indicates the length of the password (16 bytes max). This is followed by the password itself, followed by 3 check bytes. These bytes are first 3 bytes of the md5 hash calculated over the combined length and password data bytes.
TiOS can accept any binary password with 0-16 bytes of length (zero means "no password protection"). TIDE, however, always sets 16-byte passwords. These are MD5 hashes of the actual passwords you type into Enter Device Password and Change Device Password dialogs.
Here is a code snippet that verifies if the password set for the device is really DEVICE_PASSWORD ("Tibbo" in the example below). If the EEPROM data is incorrect, the code sets the right data and reboots the device. By using this sort of code in your project, you can make your devices self-protect themselves. This will spare you from manually setting the password for each device through TIDE.
'--------------------------------------------------------------------
Const DEVICE_PASSWORD="Tibbo"
'====================================================================
Sub On_sys_init()
Dim s As String(16)
Dim s2 As String(20)
Dim check As String(3)
Dim x As Byte
'take the hash of password (this is what is supposed to be stored)
s=md5(DEVICE_PASSWORD,"",MD5_FINISH,Len(DEVICE_PASSWORD))
check=md5(chr(16)+s,"",MD5_FINISH,17)
'see if this password is already set
x=stor.base 'will restore the original value later
stor.base=9 'this is where password data recides
s2=stor.getdata(1,20)
If asc(mid(s2,1,1))<>16 Or mid(s2,2,16)<>s Or mid(s2,18,3)<>check Then
'password data in the EEPROM is incorrect! -- set the password again
stor.setdata(chr(16)+s+check,1)
sys.reboot
End If
stor.base=x
...