Read the default MAC address in Linux
GuideMAC (Media Access Control) allows a computer to define and identify the locations of network devices. MAC is inevitable in Embedded linux learning. This article mainly describes how to read the MAC address in the NIC of an embedded linux device through OTP!
I. ApplicabilityHere we mainly introduce the method of reading the MAC address of the NIC, suitable for the EasyARM-i.MX287A Development Kit, its application principle and supporting examples are also applicable to the product models listed in Table 1.1 below.
Ii. PrinciplesMedia Access Control (MAC) is used to define the location of network devices. The identifier of each site on the Internet. It is expressed in hexadecimal notation and contains six bytes (48 bits ). Among them, the first three bytes are the code (up to 24 bits) allocated to different manufacturers by the IEEE Registrar RA ), it is also known as "Organization uniquely identified Identifier". The last three bytes (24 bits at low bits) are automatically assigned to the adapter interface of production by each manufacturer, it is called an extension identifier (uniqueness ). An address block can generate 2 ^ 24 different addresses. The MAC address is actually the adapter address or adapter identifier. MAC is globally unique.
"Default MAC of NIC" is a function developed by Guangzhou Zhiyuan Electronics Co., Ltd. for Awork Development Kit and M283 and M287 industrial control core board. The MAC address of NIC is stored through the chip OTP, the first two bytes of the MAC address of our factory core board and development kit are fixed as "0x00" and "0x04", and the last four bytes are stored in OTP, this document describes how to read the MAC address of the NIC by operating OTP.
Configure the OTP option in the kernel. After the compiled kernel is started, OTP device files will be exported to the file system to read information by operating these device files. The registered Nic MAC information can be first solidified into OTP and can be read at the application layer. The low 4 bits of the MAC0 address can be read through the device file "/sys/fsl_otg/SBK0". The low 4 bits of the MAC1 address can be read through the device file "/sys/fsl_otg/SBK1.
Note: If the M287 and M283 work control boards are used, you do not need to configure the kernel.
Iii. Technical ImplementationKernel Compilation
1. Configure the kernelEnter the linux-2.6.35.3 directory. Run make menuconfig to configure the kernel. The configuration steps are as follows:
sok@zlgmcu:~$ cd linux-2.6.35.3/sok@zlgmcu:~/linux-2.6.35.3$ make menuconfig
Press enter to enter the Device Drivers option, as shown in 1.
Figure 1 Kernel configuration
Press the Enter key To Go To The Chracter devices option. See figure 2.
Figure 2 Kernel configuration
Press the Space key and select <*> Freescale On-Chip OTP Memory Support option. 3. Kernel configuration figure 3.
Figure 3 Kernel configuration
Press enter to save and exit.
2. Compile the kernelRun the following command in the current directory to compile the kernel:
vmuser@Linux-host:~/ linux-2.6.35.3$ make cleanvmuser@Linux-host:~/ linux-2.6.35.3$ make uImage
The kernel firmware is located in "arch/arm/boot/uImage", and the generated kernel firmware is written to NANDFlash through the TF card.
4. Read MAC operationsThe following describes how to read the MAC of a NIC. The essence of this operation is to operate OTP device files.
Run the MAC script fileCopy the MAC. sh file in the directory of this document to the EasyARM-i.MX287A development kit through a USB flash drive or TF card, modify the File Permission to execute the permission, refer to the Command below:
root@EasyARM-iMX28x ~# chmod 777 MAC.shroot@EasyARM-iMX28x ~# ./MAC.sh
View the network device, as shown in mac5.
Figure 5 network device Diagram
View the "/sys/fsl_otp/HW_OCOTP_SRK1" and "/sys/fsl_otp/HW_OCOTP_SRK0" devices. It can be found that the four low MAC bits of the NIC are actually the values stored in OTP, as shown in figure 6.
Figure 6 OTP read Diagram
From: http://www.eepw.com.cn/article/201606/292409.htm
Address: http://www.linuxprobe.com/linux-default-mac.html