OpenWrt permanently modifies MAC address source code

Source: Internet
Author: User
*
* Flash MAC
* Copyright (C) 2014 Wrtnode machine team.
* This are free software; You can redistribute it and/or modify
*
* Cross-compile with Cross-gcc-i/path/to/cross-kernel/include
*/

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <fcntl.h>
#include <sys/ioctl.h>
#include <sys/types.h>
#include <unistd.h>
#include <stdint.h>
#define Memerase _iow (' M ', 2, struct erase_info_user)

struct Erase_info_user {
uint32_t start;
uint32_t length;
};

int main (int argc,char *argv[])
{
int fd,sz,i,offset=0x04;
int mac[6];
SZ = 0x10000;
unsigned char *buf;
BUF = (unsigned char *) malloc (SZ);
for (i=0;i<6;i++) {
Mac[i] = Strtol (argv[i+1], NULL, 16);
}
if (mac[0]%4!=0)
{
printf ("Mac[0]%4==0,please input a Effective Mac again\n");
return-1;
}
FD = open ("/dev/mtd2", O_RDWR | O_sync);
if (FD < 0) {
fprintf (stderr, "failed to open mtd2\n");
Free (BUF);
return-1;
}
Read-----------
Lseek (FD, 0, Seek_set);
Backup
If Read (FD, buf, SZ)!= sz) {
fprintf (stderr, "failed to read%d bytes from mtd2\n",
SZ);
Free (BUF);
Close (FD);
return-1;
}
Erase
struct Erase_info_user ei;
Lseek (FD, 0, Seek_set);
Ei.start = 0;
Ei.length = sz;
if (IOCTL (FD, Memerase, &ei) < 0) {
fprintf (stderr, "failed to erase mtd2\n");
Free (BUF);
Close (FD);
return-1;
}
for (i=0;i<6;i++) {
* (buf + (offset) + i) = Mac[i];
}
Write
Lseek (FD, 0, Seek_set);
If write (FD, buf, sz) = = 1) {
fprintf (stderr, "failed to write mtd%d\n", i);
Free (BUF);
Close (FD);
return-1;
}
Free (BUF);
Close (FD);
return 0;
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.