Added utility to run after first boot to set lan network address to cfe address automatically (not enabled by default)

This commit is contained in:
Martin Schröder 2015-09-15 15:11:51 +02:00
parent e8703332a6
commit dacf606c3b
4 changed files with 99 additions and 0 deletions

View file

@ -0,0 +1,41 @@
#
# Author: Martin K. Schröder <mkschreder.uk@gmail.com> 2015
#
include $(TOPDIR)/rules.mk
include $(INCLUDE_DIR)/kernel.mk
PKG_NAME:=network-address-from-cfe
PKG_VERSION:=0.0.1
PKG_RELEASE:=1
PKG_BUILD_DIR := $(BUILD_DIR)/$(PKG_NAME)-$(PKG_VERSION)
include $(INCLUDE_DIR)/package.mk
LDFLAGS+= \
-Wl,-rpath-link=$(STAGING_DIR)/usr/lib \
-Wl,-rpath-link=$(STAGING_DIR)/lib
define Package/network-address-from-cfe
CATEGORY:=Utilities
DEPENDS:=
TITLE:=utility to retreive default ip address from cfe nvram and configure lan to use that address by default
endef
define Package/network-address-from-cfe/description
network-address-from-cfe will set your lan ip address to the ip address found in cfe on broadcom box.
endef
define Build/Prepare
mkdir -p $(PKG_BUILD_DIR)
$(CP) ./src/* $(PKG_BUILD_DIR)/
endef
define Package/network-address-from-cfe/install
$(INSTALL_DIR) $(1)/sbin/
$(INSTALL_DIR) $(1)/etc/rc.d/
$(INSTALL_BIN) $(PKG_BUILD_DIR)/network-address-from-cfe $(1)/sbin/
$(INSTALL_BIN) ./network-address-from-cfe.init $(1)/etc/rc.d/S13network-address-from-cfe-first-boot
endef
$(eval $(call BuildPackage,network-address-from-cfe))

View file

@ -0,0 +1,7 @@
#!/bin/sh /etc/rc.common
/sbin/network-address-from-cfe > /dev/console
echo "network-address-from-cfe: removing program after first boot after upgrade." > /dev/console
rm /sbin/network-address-from-cfe
rm /etc/rc.d/S13network-address-from-cfe-first-boot

View file

@ -0,0 +1,2 @@
all:
$(CC) $(CFLAGS) -std=c99 -Wall -Werror -o network-address-from-cfe main.c -lc

View file

@ -0,0 +1,49 @@
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <memory.h>
#include <fcntl.h>
#include <sys/stat.h>
#define PROG "network-address-from-cfe"
// ip address is at offset 0x586 in partition 3 (/dev/mtd3) on broadcom inteno box
// this program will run once after upgrade and will then be deleted by the startup
// script. It will simply read the ip address and set the address to cfe address upon
// first boot.
int main(void){
char buffer[0xfff];
memset(buffer, 0, sizeof(buffer));
printf("%s: searching for ip address in nvram..\n", PROG);
int file = open("/dev/mtd3", O_RDONLY);
if(file < 0) {
perror(PROG);
return 0;
}
int count = read(file, buffer, sizeof(buffer));
if(count <= 0){
perror(PROG);
return 0;
}
close(file);
char ipaddr[16] = {0};
if(strncmp(buffer + 0x584, "e=", 2) != 0){
printf("%s: could not find ip address in flash\n", PROG);
return 0;
}
int cc = 0;
for(char *ch = buffer + 0x586; *ch != ':' && cc < sizeof(ipaddr); ch++, cc++){
ipaddr[cc] = *ch;
}
printf("%s: using ip address %s\n", PROG, ipaddr);
char command[255];
snprintf(command, sizeof(command), "uci set network.lan.ipaddr=%s", ipaddr);
system(command);
system("uci commit");
system("ubus call network reload");
return 0;
}