Modify Mac Address

Discussion and questions about programming with Ultibo.
ykcan
Posts: 2
Joined: Mon Aug 05, 2019 12:43 pm

Modify Mac Address

Postby ykcan » Mon Aug 05, 2019 1:12 pm

Hi Ultibo Team,

I would like to learn how to modify EEPROM data(Mac address) with LAN9512?
Is there macro or library to read and write EEPROM data?

I don't know to upload image(image URL), so ...
CM3L <--> LAN9514 <--> LAN9512 <--> EEPROM

thank for your supports

Best regards
ykcan
User avatar
Ultibo
Site Admin
Posts: 2303
Joined: Sat Dec 19, 2015 3:49 am
Location: Australia

Re: Modify Mac Address

Postby Ultibo » Tue Aug 06, 2019 12:25 am

ykcan wrote:I would like to learn how to modify EEPROM data(Mac address) with LAN9512?
Is there macro or library to read and write EEPROM data?

Hi ykcan, welcome to Ultibo.

Do you want to modify the EEPROM Mac address so that the device always defaults to the address you set or do you just want to change the Mac address the device uses while it is running?

The standard Raspberry Pi (A/B/Zero/2/3 etc) doesn't have an EEPROM at all so the address used by the LAN9512 or LAN9514 is always set during boot and can easily be changed.

If you are designing a board using the CM3L and want to include a LAN9512 with an EEPROM then the functions needed to read and write the EEPROM data can be found in the Linux smsc95xx driver (look for smsc95xx_read_eeprom and smsc95xx_write_eeprom), we haven't added them to Ultibo yet because we don't have any way to test them but they should be simple enough to implement if needed.

If you just want to set the Mac address that the LAN9512/9514 uses when running under Ultibo let us know and we'll provide info on a couple of ways you can do that.
Ultibo.org | Make something amazing
https://ultibo.org
ykcan
Posts: 2
Joined: Mon Aug 05, 2019 12:43 pm

Re: Modify Mac Address

Postby ykcan » Tue Aug 06, 2019 2:33 am

Hi Ultibo Team

I want change the Mac address that the LAN9512/9514 uses when running under Ultibo, let me know how to change
Image

Best regards
ykcan
User avatar
Ultibo
Site Admin
Posts: 2303
Joined: Sat Dec 19, 2015 3:49 am
Location: Australia

Re: Modify Mac Address

Postby Ultibo » Wed Aug 07, 2019 1:31 am

ykcan wrote:I want change the Mac address that the LAN9512/9514 uses when running under Ultibo, let me know how to change

The first option is to use an InitUnit, which we have shown in a number of other examples, to set the value of SMSC95XX_MAC_ADDRESS during startup to the Mac address you want to use. The InitUnit would look something like this:

Code: Select all

unit InitUnit;

interface

uses GlobalConfig;

implementation


initialization

 SMSC95XX_MAC_ADDRESS := 'B827EB123456';
 
end.

and you would add InitUnit as the very first entry in the uses clause of your project file.

That works if you always want to use the same hard coded Mac address but it is also possible to set the Mac address in code by shutting down the interface, changing the Mac and then enabling the interface again, we're just putting together a simple example that shows how to do that which we'll post here as soon as it is completed.
Ultibo.org | Make something amazing
https://ultibo.org
User avatar
Ultibo
Site Admin
Posts: 2303
Joined: Sat Dec 19, 2015 3:49 am
Location: Australia

Re: Modify Mac Address

Postby Ultibo » Wed Aug 07, 2019 10:53 am

Ultibo wrote:That works if you always want to use the same hard coded Mac address but it is also possible to set the Mac address in code by shutting down the interface, changing the Mac and then enabling the interface again, we're just putting together a simple example that shows how to do that which we'll post here as soon as it is completed.

So the second option is to set the Mac address at run time by directly controlling the network interfaces, we've modified our earlier example that showed how to set the IP address in code and made it change the Mac address instead.

Code: Select all

uses
  SysUtils,
  Classes,
  GlobalSock,
  Network,
  Transport,
  IP;

function SetMACAddress(const AName,AAddress:String):Boolean;
var
 Adapter:TNetworkAdapter;
 Transport:TNetworkTransport;
 TransportAdapter:TIPTransportAdapter;
begin
 {}
 Result:=False;
 
 if Length(AName) = 0 then Exit;
 if Length(AAddress) = 0 then Exit;
 
 {Get Adapter}
 Adapter:=AdapterManager.GetAdapterByName(AName,True,NETWORK_LOCK_READ);
 if Adapter = nil then Exit;
 try
  {Get IP Transport}
  Transport:=TransportManager.GetTransportByType(AF_INET,PACKET_TYPE_IP,True,NETWORK_LOCK_READ);
  if Transport = nil then Exit;
  try
   {Get Transport Adapter (The connection between the Network device and the IP Transport)}
   TransportAdapter:=TIPTransportAdapter(Transport.GetAdapterByAdapter(Adapter,True,NETWORK_LOCK_WRITE));
   if TransportAdapter = nil then Exit;

   {Set the Adapter Status to Down}
   TransportAdapter.Adapter.Status:=ADAPTER_STATUS_DOWN;

   {Unlock Transport Adapter}
   TransportAdapter.WriterUnlock;

   WriteLn(AName + ' set to ADAPTER_STATUS_DOWN');

   WriteLn('Waiting until Transport Adapter is Unconfigured');

   {Lock Transport Adapter (Reader)}
   TransportAdapter.ReaderLock;

   {Wait for Transport Adapter to be Unconfigured}
   while TransportAdapter.Configured do
    begin
     {Wait}
     Sleep(500);
    end;

   {Unlock Adapter}
   Adapter.ReaderUnlock;

   WriteLn('Setting Adapter MAC Address');

   {Set the MAC address}
   Adapter.SetHardwareAddress(INVALID_HANDLE_VALUE,StringToHardwareAddress(AAddress));

   {Lock Adapter (Reader)}
   Adapter.ReaderLock;

   {Unlock Transport Adapter}
   TransportAdapter.ReaderUnlock;

   {Lock Transport Adapter (Writer)}
   TransportAdapter.WriterLock;

   WriteLn('Setting Transport MAC Address');

   {Set the MAC address}
   TransportAdapter.Hardware:=StringToHardwareAddress(AAddress);

   {Set the Adapter Status to Up}
   TransportAdapter.Adapter.Status:=ADAPTER_STATUS_UP;

   {Unlock Transport Adapter}
   TransportAdapter.WriterUnlock;
   
   WriteLn(AName + ' set to ADAPTER_STATUS_UP');

   Result:=True;
  finally
   Transport.ReaderUnlock;
  end;
 finally
  Adapter.ReaderUnlock;
 end;
end;

As with the original example it's important to maintain all the lock handling as shown to avoid the potential of crashing the network layer.

You can use this function in your program by inserting a simple call like this to specify the Mac address you want:

Code: Select all

 SetMACAddress('Network0','B827EB123456');

Let us know if that does what you need.
Ultibo.org | Make something amazing
https://ultibo.org
User avatar
Ultibo
Site Admin
Posts: 2303
Joined: Sat Dec 19, 2015 3:49 am
Location: Australia

Re: Modify Mac Address

Postby Ultibo » Wed Aug 21, 2019 12:15 am

Just to update this topic, we've committed a change in Ultibo Core 2.0.723 which allows specifying the Mac address in the cmdline.txt file in much the same way you can specify a static IP or other configuration options.

So now you can add something like this:

Code: Select all

NETWORK0_HARDWARE_ADDRESS=B827EB123456

to the cmdline.txt file (along with any other settings you require) and it will be applied by the network stack during startup.

You can of course specify Mac addresses for multiple network adapters such as NETWORK0, NETWORK1, NETWORK2 and so on.

One final note, this doesn't work in QEMU. While Ultibo supports command line parameters being passed, the emulated network adapter in QEMU doesn't support setting the Mac address at all.
Ultibo.org | Make something amazing
https://ultibo.org

Return to “General”

Who is online

Users browsing this forum: No registered users and 34 guests