diff --git a/package/boot/rtk-init/Makefile b/package/boot/rtk-init/Makefile new file mode 100644 index 0000000000..7b7b033e47 --- /dev/null +++ b/package/boot/rtk-init/Makefile @@ -0,0 +1,55 @@ +# +# Copyright (C) 2020 jjm2473 +# +# This is free software, licensed under the GNU General Public License v2. +# See /LICENSE for more information. +# + +include $(TOPDIR)/rules.mk + +PKG_NAME:=rtk-init +PKG_RELEASE:=1 +PKG_LICENSE:=CC0-1.0 +PKG_MAINTAINER:=jjm2473 + +PKG_BUILD_DIR := $(BUILD_DIR)/$(PKG_NAME) + +PKG_FLAGS:=nonshared +include $(INCLUDE_DIR)/package.mk + +define Package/rtk-init + CATEGORY:=Boot Loaders + SECTION:=boot + TITLE:=realtek openwrt+android double os init + MAINTAINER:=jjm2473 + DEPENDS:=@TARGET_realtek +endef + +define Package/rtk-init/description + This package contains init program for realtek to boot to openwrt+android double os. +endef + +define Build/Configure +endef + +define Build/Compile + $(MAKE) -C $(PKG_BUILD_DIR) \ + CC="$(TARGET_CC)" \ + CFLAGS="$(TARGET_CFLAGS) -Wall" \ + LDFLAGS="$(TARGET_LDFLAGS)" +endef + +define Package/rtk-init/install + $(INSTALL_DIR) $(1)/etc + $(INSTALL_BIN) $(PKG_BUILD_DIR)/init $(1)/etc/ + $(INSTALL_DIR) $(1)/etc/init.d + $(INSTALL_BIN) ./files/android.init $(1)/etc/init.d/android + $(INSTALL_BIN) ./files/printk.init $(1)/etc/init.d/printk + $(INSTALL_DIR) $(1)/etc/uci-defaults + $(INSTALL_BIN) ./files/10_disable_services $(1)/etc/uci-defaults/ + $(INSTALL_BIN) ./files/10_fix_service_status $(1)/etc/uci-defaults/ + $(INSTALL_DIR) $(1)/etc/profile.d + $(INSTALL_BIN) ./files/00_rtk_android.sh $(1)/etc/profile.d/ +endef + +$(eval $(call BuildPackage,rtk-init)) diff --git a/package/boot/rtk-init/files/00_rtk_android.sh b/package/boot/rtk-init/files/00_rtk_android.sh new file mode 100755 index 0000000000..75575e106a --- /dev/null +++ b/package/boot/rtk-init/files/00_rtk_android.sh @@ -0,0 +1 @@ +[ -f /tmp/.android ] && export ANDROID=1 \ No newline at end of file diff --git a/package/boot/rtk-init/files/10_disable_services b/package/boot/rtk-init/files/10_disable_services new file mode 100755 index 0000000000..9645adc629 --- /dev/null +++ b/package/boot/rtk-init/files/10_disable_services @@ -0,0 +1,12 @@ +#!/bin/sh + +if [ -f /rom/android/.ottwifi ]; then + for service in dnsmasq firewall; do + [ -f /etc/init.d/$service ] && ( + /etc/init.d/$service disable + /etc/init.d/$service stop + ) + done +fi + +exit 0 diff --git a/package/boot/rtk-init/files/10_fix_service_status b/package/boot/rtk-init/files/10_fix_service_status new file mode 100755 index 0000000000..07a221b3e8 --- /dev/null +++ b/package/boot/rtk-init/files/10_fix_service_status @@ -0,0 +1,47 @@ +#!/bin/sh + +[ -f /tmp/.android ] || exit 0 + +if [ -e /etc/config/minidlna ]; then + /sbin/uci set minidlna.@minidlna[0].interface='br-lan,eth0,wlan0' + /sbin/uci commit minidlna +fi + +[ -f /etc/config/samba -a ! -L /etc/config/samba ] && ln -s samba /etc/config/samba4 +[ -f /etc/config/samba4 -a ! -L /etc/config/samba4 ] && ln -s samba4 /etc/config/samba + +# just procded init script should be set enabled flag by init status. +NO_ST_SERVICES="samba samba4" + +for service in $NO_ST_SERVICES; do + init=$service + [ "$init" == "vsftpd" -a -f /etc/init.d/vsftpd-uci ] && init="vsftpd-uci" + if [ -e /etc/init.d/$init ]; then + [ -e /etc/config/$service ] || touch /etc/config/$service + SECTION=$service + [ "$SECTION" == "samba4" ] && SECTION="samba" + [ "$SECTION" == "afpd" ] && SECTION="netatalk" + uci rename $service.@$SECTION[0]='config' >>/dev/null 2>&1 + uci set $service.config=$SECTION + if /etc/init.d/$init enabled; then + /sbin/uci set $service.config.enabled=1 + else + /sbin/uci set $service.config.enabled=0 + fi + /sbin/uci commit $service + fi +done + +for service in vsftpd minidlna forked-daapd transmission netatalk; do + if [ -e /etc/init.d/$service ]; then + [ -e /etc/config/$service ] || touch /etc/config/$service + if ! uci get $service.config.enabled >>/dev/null 2>&1; then + uci rename $service.@$service[0]='config' >>/dev/null 2>&1 + uci set $service.config=$service + uci set $service.config.enabled=0 + uci commit $service + fi + fi +done + +exit 0 diff --git a/package/boot/rtk-init/files/android.init b/package/boot/rtk-init/files/android.init new file mode 100755 index 0000000000..756bb3f8c4 --- /dev/null +++ b/package/boot/rtk-init/files/android.init @@ -0,0 +1,22 @@ +#!/bin/sh /etc/rc.common +# 2020 jjm2473 porting openwrt 19.07+ to zidoo z9s + +START=01 + +boot() { + if [ -n "$ANDROID" ]; then + # notify android continue + touch /android/.coldplug_done + touch /rom/android/.coldplug_done + # let others know we are running with android + touch /tmp/.android + chmod 444 /tmp/.android + if [ -f /rom/android/.ottwifi ]; then + # dns will be configured by android through rpcd-mod-file + rm /etc/resolv.conf + touch /etc/resolv.conf + chmod 644 /etc/resolv.conf + return 0 + fi + fi +} diff --git a/package/boot/rtk-init/files/printk.init b/package/boot/rtk-init/files/printk.init new file mode 100755 index 0000000000..54e140be5a --- /dev/null +++ b/package/boot/rtk-init/files/printk.init @@ -0,0 +1,11 @@ +#!/bin/sh /etc/rc.common +# 2020 jjm2473 porting openwrt 19.07+ to zidoo z9s + +START=99 + +boot() { + if [ -n "$ANDROID" ]; then + # tune printk level after success boot, avoid noise in console + echo '4 5 1 7' > /proc/sys/kernel/printk + fi +} diff --git a/package/boot/rtk-init/src/Makefile b/package/boot/rtk-init/src/Makefile new file mode 100644 index 0000000000..f4c3681fcf --- /dev/null +++ b/package/boot/rtk-init/src/Makefile @@ -0,0 +1,15 @@ +CC ?= gcc +CFLAGS ?= -Wall +MAIN_OBJ = main.o +OBJS = cmdline.o + +all: init + +%.o: %.c + $(CC) $(CFLAGS) -c -o $@ $< + +init: $(OBJS) $(MAIN_OBJ) + $(CC) $(LDFLAGS) -o $@ $(OBJS) $(MAIN_OBJ) + +clean: + rm -f init *.o diff --git a/package/boot/rtk-init/src/android_filesystem_config.h b/package/boot/rtk-init/src/android_filesystem_config.h new file mode 100644 index 0000000000..c7eeeaddb8 --- /dev/null +++ b/package/boot/rtk-init/src/android_filesystem_config.h @@ -0,0 +1,115 @@ +/* + * https://android.googlesource.com/platform/system/core/+/refs/tags/android-6.0.1_r31/include/private/android_filesystem_config.h + */ + +#ifndef _ANDROID_FILESYSTEM_CONFIG_H_ +#define _ANDROID_FILESYSTEM_CONFIG_H_ + +/* This is the master Users and Groups config for the platform. + * DO NOT EVER RENUMBER + */ +#define AID_ROOT 0 /* traditional unix root user */ +#define AID_SYSTEM 1000 /* system server */ +#define AID_RADIO 1001 /* telephony subsystem, RIL */ +#define AID_BLUETOOTH 1002 /* bluetooth subsystem */ +#define AID_GRAPHICS 1003 /* graphics devices */ +#define AID_INPUT 1004 /* input devices */ +#define AID_AUDIO 1005 /* audio devices */ +#define AID_CAMERA 1006 /* camera devices */ +#define AID_LOG 1007 /* log devices */ +#define AID_COMPASS 1008 /* compass device */ +#define AID_MOUNT 1009 /* mountd socket */ +#define AID_WIFI 1010 /* wifi subsystem */ +#define AID_ADB 1011 /* android debug bridge (adbd) */ +#define AID_INSTALL 1012 /* group for installing packages */ +#define AID_MEDIA 1013 /* mediaserver process */ +#define AID_DHCP 1014 /* dhcp client */ +#define AID_SDCARD_RW 1015 /* external storage write access */ +#define AID_VPN 1016 /* vpn system */ +#define AID_KEYSTORE 1017 /* keystore subsystem */ +#define AID_USB 1018 /* USB devices */ +#define AID_DRM 1019 /* DRM server */ +#define AID_MDNSR 1020 /* MulticastDNSResponder (service discovery) */ +#define AID_GPS 1021 /* GPS daemon */ +#define AID_UNUSED1 1022 /* deprecated, DO NOT USE */ +#define AID_MEDIA_RW 1023 /* internal media storage write access */ +#define AID_MTP 1024 /* MTP USB driver access */ +#define AID_UNUSED2 1025 /* deprecated, DO NOT USE */ +#define AID_DRMRPC 1026 /* group for drm rpc */ +#define AID_NFC 1027 /* nfc subsystem */ +#define AID_SDCARD_R 1028 /* external storage read access */ +#define AID_CLAT 1029 /* clat part of nat464 */ +#define AID_LOOP_RADIO 1030 /* loop radio devices */ +#define AID_MEDIA_DRM 1031 /* MediaDrm plugins */ +#define AID_PACKAGE_INFO 1032 /* access to installed package details */ +#define AID_SDCARD_PICS 1033 /* external storage photos access */ +#define AID_SDCARD_AV 1034 /* external storage audio/video access */ +#define AID_SDCARD_ALL 1035 /* access all users external storage */ +#define AID_LOGD 1036 /* log daemon */ +#define AID_SHARED_RELRO 1037 /* creator of shared GNU RELRO files */ +#define AID_DBUS 1038 /* dbus-daemon IPC broker process */ +#define AID_TLSDATE 1039 /* tlsdate unprivileged user */ +#define AID_MEDIA_EX 1040 /* mediaextractor process */ +#define AID_AUDIOSERVER 1041 /* audioserver process */ +#define AID_METRICS_COLL 1042 /* metrics_collector process */ +#define AID_METRICSD 1043 /* metricsd process */ +#define AID_WEBSERV 1044 /* webservd process */ +#define AID_DEBUGGERD 1045 /* debuggerd unprivileged user */ +#define AID_MEDIA_CODEC 1046 /* mediacodec process */ +#define AID_CAMERASERVER 1047 /* cameraserver process */ +#define AID_FIREWALL 1048 /* firewalld process */ +#define AID_TRUNKS 1049 /* trunksd process (TPM daemon) */ +#define AID_NVRAM 1050 /* Access-controlled NVRAM */ +#define AID_DNS 1051 /* DNS resolution daemon (system: netd) */ +#define AID_DNS_TETHER 1052 /* DNS resolution daemon (tether: dnsmasq) */ +#define AID_WEBVIEW_ZYGOTE 1053 /* WebView zygote process */ +#define AID_VEHICLE_NETWORK 1054 /* Vehicle network service */ +#define AID_MEDIA_AUDIO 1055 /* GID for audio files on internal media storage */ +#define AID_MEDIA_VIDEO 1056 /* GID for video files on internal media storage */ +#define AID_MEDIA_IMAGE 1057 /* GID for image files on internal media storage */ +#define AID_TOMBSTONED 1058 /* tombstoned user */ +#define AID_MEDIA_OBB 1059 /* GID for OBB files on internal media storage */ +#define AID_ESE 1060 /* embedded secure element (eSE) subsystem */ +#define AID_OTA_UPDATE 1061 /* resource tracking UID for OTA updates */ +/* Changes to this file must be made in AOSP, *not* in internal branches. */ +#define AID_SHELL 2000 /* adb and debug shell user */ +#define AID_CACHE 2001 /* cache access */ +#define AID_DIAG 2002 /* access to diagnostic resources */ +/* The range 2900-2999 is reserved for OEM, and must never be + * used here */ +#define AID_OEM_RESERVED_START 2900 +#define AID_OEM_RESERVED_END 2999 +/* The 3000 series are intended for use as supplemental group id's only. + * They indicate special Android capabilities that the kernel is aware of. */ +#define AID_NET_BT_ADMIN 3001 /* bluetooth: create any socket */ +#define AID_NET_BT 3002 /* bluetooth: create sco, rfcomm or l2cap sockets */ +#define AID_INET 3003 /* can create AF_INET and AF_INET6 sockets */ +#define AID_NET_RAW 3004 /* can create raw INET sockets */ +#define AID_NET_ADMIN 3005 /* can configure interfaces and routing tables. */ +#define AID_NET_BW_STATS 3006 /* read bandwidth statistics */ +#define AID_NET_BW_ACCT 3007 /* change bandwidth statistics accounting */ +#define AID_READPROC 3009 /* Allow /proc read access */ +#define AID_WAKELOCK 3010 /* Allow system wakelock read/write access */ +/* The range 5000-5999 is also reserved for OEM, and must never be used here. */ +#define AID_OEM_RESERVED_2_START 5000 +#define AID_OEM_RESERVED_2_END 5999 +#define AID_EVERYBODY 9997 /* shared between all apps in the same profile */ +#define AID_MISC 9998 /* access to misc storage */ +#define AID_NOBODY 9999 +#define AID_APP 10000 /* TODO: switch users over to AID_APP_START */ +#define AID_APP_START 10000 /* first app user */ +#define AID_APP_END 19999 /* last app user */ +#define AID_CACHE_GID_START 20000 /* start of gids for apps to mark cached data */ +#define AID_CACHE_GID_END 29999 /* end of gids for apps to mark cached data */ +#define AID_EXT_GID_START 30000 /* start of gids for apps to mark external data */ +#define AID_EXT_GID_END 39999 /* end of gids for apps to mark external data */ +#define AID_EXT_CACHE_GID_START 40000 /* start of gids for apps to mark external cached data */ +#define AID_EXT_CACHE_GID_END 49999 /* end of gids for apps to mark external cached data */ +#define AID_SHARED_GID_START 50000 /* start of gids for apps in each user to share */ +#define AID_SHARED_GID_END 59999 /* end of gids for apps in each user to share */ +#define AID_ISOLATED_START 99000 /* start of uids for fully isolated sandboxed processes */ +#define AID_ISOLATED_END 99999 /* end of uids for fully isolated sandboxed processes */ +#define AID_USER 100000 /* TODO: switch users over to AID_USER_OFFSET */ +#define AID_USER_OFFSET 100000 /* offset for uid ranges for each user */ + +#endif // _ANDROID_FILESYSTEM_CONFIG_H_ \ No newline at end of file diff --git a/package/boot/rtk-init/src/cmdline.c b/package/boot/rtk-init/src/cmdline.c new file mode 100644 index 0000000000..c76047cfed --- /dev/null +++ b/package/boot/rtk-init/src/cmdline.c @@ -0,0 +1,65 @@ +/** + * reverse enginering of zidoo z9s 2.1.30 /etc/init (sha256sum 9b3f9648460c15d345986b3246e0471057fb7d4af17fbba0b654f856e1a13e2a) + */ +#define _GNU_SOURCE + +#include +#include +#include +#include +#include +#include +#include + +#define BUF_SIZE 0x800 + +char CMDLINE[BUF_SIZE+1];// 0x412348 + +char *get_cmdline(const char *key, char *dest, int max) { + if (CMDLINE[0] == '\0') { + mount("proc", "/proc", "proc", MS_NOATIME|MS_NOEXEC|MS_NODEV|MS_NOSUID, NULL); + int fd = open("/proc/cmdline", O_RDONLY); + ssize_t len = read(fd, CMDLINE, BUF_SIZE); + close(fd); + umount("/proc"); + if (len > 0) { // 0x0040148c + // 0x40149c + CMDLINE[len] = '\0'; + } else { + // 0x401494 + return NULL; // 0x1548 + } + } + if (dest != NULL) { // 0x14a0 + // 0x14a4 + *dest = '\0'; + char *ptr = NULL; + char *kv = strtok_r(CMDLINE, " \t\n", &ptr); // x20 + size_t maxlen = max; + while (kv != NULL) { + // 0x14d0 + char *ke = strchr(kv, '='); + size_t klen = ke - kv; + kv[strlen(kv)] = ' '; + if (klen >= 0 && !strncmp(key, kv, klen) && !key[klen]) { + // 0x1510 + strncpy(dest, ke + 1, maxlen); //0x151c + dest[maxlen - 1] = '\0'; //0x1520 + } + // 0x1524 + kv = strtok_r(NULL, " \t\n", &ptr); + // 0x00401538 : b 0x4014cc + //continue; + } + // 0x153c + if (dest[0] != '\0') { + return dest; + } + // else { + // // 0x1494 + // return NULL; + // } + } + // 0x1494 + return NULL;// 0x1548 +} \ No newline at end of file diff --git a/package/boot/rtk-init/src/cmdline.h b/package/boot/rtk-init/src/cmdline.h new file mode 100644 index 0000000000..0e27447438 --- /dev/null +++ b/package/boot/rtk-init/src/cmdline.h @@ -0,0 +1,7 @@ + +#ifndef RTD1295_INIT_CMDLINE_H +#define RTD1295_INIT_CMDLINE_H 1 + +char *get_cmdline(const char *key, char *dest, int max); + +#endif \ No newline at end of file diff --git a/package/boot/rtk-init/src/main.c b/package/boot/rtk-init/src/main.c new file mode 100644 index 0000000000..c162dbc0b9 --- /dev/null +++ b/package/boot/rtk-init/src/main.c @@ -0,0 +1,227 @@ +/** + * reverse enginering of zidoo z9s 2.1.30 /etc/init (sha256sum 9b3f9648460c15d345986b3246e0471057fb7d4af17fbba0b654f856e1a13e2a) + */ +#define _GNU_SOURCE + +#include +#include +#include +#include + +#include +#include +#include +#include +#include + +#include +#include +#include + +#include +#include // setns + +#include // setgroups + +#include // makedev + +#include "cmdline.h" + +#include "android_filesystem_config.h" + +#define ANDROID_ROOT "/android" +#define ANDROID_VIFACE "veth1" +#define ANDROID_VIFACE_NAME "eth9" +#define NET_NS_DIR "/var/run/netns" +#define ANDROID_NET_NS "androidnet" + +#define OPENWRT_INIT "/etc/preinit" + +enum console { + CONSOLE_ANDROID, + CONSOLE_OPENWRT +}; + +const gid_t ROOT_GROUPS[] = {AID_SDCARD_RW, AID_MEDIA_RW, AID_SHELL, AID_CACHE, + AID_NET_ADMIN, AID_NET_BW_ACCT}; + +#define EXEC(pre, cmd) argv[0] = cmd;\ + pre execv(cmd, argv); + +static char *prepare(char **argv); + +int main(int argc, char **argv, char **envp) { + char *exec_cmd = prepare(argv); + EXEC(return, exec_cmd) +} + +static char *prepare(char **argv) { + int wstatus; + int *sp58 = &wstatus; + struct stat sb; + struct stat *sp80 = &sb; + char sp100[0x80]; + char sp180[0x80]; + char *cfg = get_cmdline("androidboot.hardware", sp100, 0x80); + + enum console console = CONSOLE_ANDROID; + if (cfg != NULL && strncmp(cfg, "kylin", 0x5) == 0) { + fputs("===== OpenWRT + Android ===== \n", stderr); + cfg = get_cmdline("console.switch", sp100, 0x80); + if (cfg != NULL && strncmp(cfg, "openwrt", 0x7) == 0) { + console = CONSOLE_OPENWRT; + } + int router = 0; + memset(sp180, 0, sizeof(sp180)); + if (stat("/mnt/android/.ottwifi", sp80) != 0) { + // if /mnt/android/.ottwifi not exists + router = 1; + } + + mount("tmpfs", ANDROID_ROOT, "tmpfs", MS_NOATIME, "size=10240k"); + putenv("PATH=/usr/sbin:/usr/bin:/sbin:/bin"); + + snprintf(sp180, 0x80, "cp -a /mnt/android/* %s", ANDROID_ROOT); + if (system(sp180) < 0 || chdir(ANDROID_ROOT) < 0) { + fputs("Copy Android rootfs failed!\nBoot Openwrt only!\n", stderr); + umount(ANDROID_ROOT); + return OPENWRT_INIT; + } + if (router == 0 && get_cmdline("net.switch", sp100, 0x80) != NULL && strncmp(sp100, "openwrt", 0x7) == 0) { + // router mode by bootargs + router = 1; + } + if (router) { + fputs("===== Router mode ===== \n", stderr); + unlink(ANDROID_ROOT "/.ottwifi"); + } else { + close(open(ANDROID_ROOT "/.ottwifi", O_CLOEXEC|O_CREAT|O_WRONLY)); + } + + mknod(ANDROID_ROOT "/dev/null", S_IFCHR|S_IRUSR|S_IWUSR|S_IRGRP|S_IWGRP|S_IROTH|S_IWOTH, makedev(1, 3)); + if (CONSOLE_ANDROID == console) { + mknod(ANDROID_ROOT "/dev/tty", S_IFCHR|S_IRUSR|S_IWUSR, makedev(5, 0)); + mknod(ANDROID_ROOT "/dev/console", S_IFCHR|S_IRUSR|S_IWUSR, makedev(5, 1)); + } + + // Openwrt + Android + pid_t cpid = fork(); // 0x400f48 + if (cpid == 0) { + chdir("/"); + // openwrt init + // 0x400f54 + setsid(); + // assign permission from android + setenv("ANDROID", "1", 1); + setgroups(sizeof(ROOT_GROUPS)/sizeof(gid_t), ROOT_GROUPS); + unshare(CLONE_NEWPID); //movz w0, 0x2000, lsl 16 + pid_t cid = fork();//0x400f60 + if (cid == 0) {//0x400f64 + //0x400f68 - 0x400f80 + EXEC(, OPENWRT_INIT) + _exit(1); + } else { //0x400f84 + //0x400f84 - 0x400f94 + waitpid(cid, sp58, 0); + exit(0); + } + } else { + // android init + // 0x00401010 + int debuggable = 1; + if (console == CONSOLE_OPENWRT) { + // set ro.debuggable=0; + debuggable = 0; + } + // else { + // // set ro.debuggable=1; + // // debuggable = 1; + // } + snprintf(sp180, 0x80, "sed -i 's,^\\(ro.debuggable=\\)\\(.*\\),\\1%d,g' %s", debuggable, "./default.prop"); + system(sp180); // 0x401078 + int ttl; + for (ttl = 101; ttl>0; --ttl) { + if (stat(".coldplug_done", sp80) == 0) { + break; + } + usleep(100000); + } + if (router) { + // router mode init + // 0x004010a8 - 0x401280 + // sp+0x78 = 0*8; + // ((int64_t *)sp58)[0] = 0;//sp+0x58 = 0*16; // char[] + // ((int64_t *)sp58)[1] = 0;//sp+0x58 = 0*16; // char[] + // sp+0x68 = 0*16; + + // sp+0x50 = 0; + // sp+0x40 = 0; + + //*sp58 = *((int *)"eth0"); + + // get mac address of eth0 + // 0x10ec + int w23 = 0xda, w24 = 0x05; + int fd = socket(AF_INET, SOCK_DGRAM, PF_UNSPEC); + if (fd >= 0) { // 0x10e8 + struct ifreq ifr; + memset(ifr.ifr_name, 0, sizeof(ifr)); + strncpy(ifr.ifr_name, "eth0", 4); + // 0x10f8 + if (ioctl(fd, SIOCGIFHWADDR, &ifr) == 0) { + // 0x110c + w23 = (ifr.ifr_hwaddr.sa_data[4] & 0xFF); // sp+0x6e; ifr+0x16 + w24 = (ifr.ifr_hwaddr.sa_data[5] & 0xFF); // sp+0x6f; ifr+0x17 + } + close(fd); + } + // else { + // // 0x10ec + // } + + // 0x401118 + //mount("proc", "/proc", "proc", MS_NOATIME|MS_NOEXEC|MS_NODEV|MS_NOSUID, NULL); + mkdir(NET_NS_DIR, S_IRWXU|S_IRGRP|S_IXGRP|S_IROTH|S_IXOTH); // mode 0755 + + // ip netns add androidnet + snprintf(sp180, 0x80, "ip netns add %s", ANDROID_NET_NS); + system(sp180); + + char veth_mac_addr[0x12]; + memset(veth_mac_addr, 0, sizeof(veth_mac_addr)); + + // ip link add veth1 addr 00:e0:4c:0b:??:?? type veth peer name eth9 addr 00:e0:4c:ab:??:?? + snprintf(veth_mac_addr, 0x12, "%02x:%02x:%02x:%02x:%02x:%02x", 0x00, 0xe0, 0x4c, 0x0b, w23, w24); + snprintf(sp180, 0x80, "ip link add %s addr %s type veth peer name %s addr ", ANDROID_VIFACE, veth_mac_addr, ANDROID_VIFACE_NAME); + veth_mac_addr[9] = 'a'; + strncat(sp180, veth_mac_addr, strlen(veth_mac_addr)); + system(sp180); + + // ip link set eth9 netns androidnet + snprintf(sp180, 0x80, "ip link set %s netns %s", ANDROID_VIFACE_NAME, ANDROID_NET_NS); + system(sp180); + + // ip netns exec androidnet ip link set dev lo up + snprintf(sp180, 0x80, "ip netns exec %s ip link set dev lo up", ANDROID_NET_NS); + system(sp180); + + snprintf(sp180, 0x80, "%s/%s", NET_NS_DIR, ANDROID_NET_NS); + fd = open(sp180, 0); + setns(fd, CLONE_NEWNET); + close(fd); + //umount("/proc"); + // 0x401280 : b 0x4012a0 + } + + // 0x004012a0 + unshare(CLONE_NEWNS|CLONE_NEWUTS); + chroot("."); + return "/init"; + //return execv("/init", ["/init"]); // 0x4012bc + } + } else { + fputs("===== OpenWRT ===== \n", stderr); + return OPENWRT_INIT; + //return execv("/etc/preinit", ["/etc/preinit"]); // 0x4012bc + } +} diff --git a/target/linux/realtek/base-files/etc/board.d/03_gpio_switches b/target/linux/realtek/base-files/etc/board.d/03_gpio_switches new file mode 100755 index 0000000000..11d2aae928 --- /dev/null +++ b/target/linux/realtek/base-files/etc/board.d/03_gpio_switches @@ -0,0 +1,22 @@ +#!/bin/sh +# +# Copyright (C) 2018 OpenWrt.org +# + +. /lib/functions/uci-defaults.sh + +board_config_update + +board=$(board_name) + +case "$board" in +Realtek,rtd-129*) + ucidef_add_gpio_switch "gpio_fan" "GPIO Fan switch" "15" "1" + ;; +*) + ;; +esac + +board_config_flush + +exit 0 diff --git a/target/linux/realtek/base-files/lib/preinit/81_console_switch b/target/linux/realtek/base-files/lib/preinit/81_console_switch new file mode 100644 index 0000000000..6c3d26b20b --- /dev/null +++ b/target/linux/realtek/base-files/lib/preinit/81_console_switch @@ -0,0 +1,12 @@ +#!/bin/sh + +do_console_switch() { + [ -e /rom/note ] && return 0 + if grep -Fq 'console.switch=android' /proc/cmdline; then + sed -i "/ttyS0/d" /etc/inittab + else + grep -Fq 'ttyS0::' /etc/inittab || echo 'ttyS0::askfirst:/usr/libexec/login.sh' >> /etc/inittab + fi +} + +[ "$INITRAMFS" = "1" ] || boot_hook_add preinit_main do_console_switch diff --git a/target/linux/realtek/base-files/usr/sbin/mount_root b/target/linux/realtek/base-files/usr/sbin/mount_root new file mode 100755 index 0000000000..4cc4c1d045 --- /dev/null +++ b/target/linux/realtek/base-files/usr/sbin/mount_root @@ -0,0 +1,100 @@ +#!/bin/sh +# Copyright (C) 2010 OpenWrt.org + +DEFAULT_OVERLAY_FS="ext4" + +get_overlay_partition_from_kernel_cmdline() +{ + OVERLAY_FS=${DEFAULT_OVERLAY_FS} + + for item in `cat /proc/cmdline`; do + case $item in + overlay=*) OVERLAY_DEV=${item:8};; + overlayfs=*) OVERLAY_FS=${item:10};; + esac + done + + [ -z "$OVERLAY_DEV" ] && return -1 + + echo "- mount overlayfs -" + + # Check device existence + [ ! -e $OVERLAY_DEV ] && \ + echo "overlay: $OVERLAY_DEV not found" && return -1 + + # Check filesystem + FSTYPE=`blkid -o value -s TYPE $OVERLAY_DEV` + [ -z "$FSTYPE" ] && \ + echo "overlay: no valid filesystem" || \ + echo "overlay: filesystem $FSTYPE found" +} + +format_overlay_partition() +{ + [ "$FSTYPE" = "$OVERLAY_FS" ] && return 0 + + echo "overlay: try to format $OVERLAY_DEV as $OVERLAY_FS partition" + + case "$OVERLAY_FS" in + ext4) + mkfs.ext4 -q -L RTKNAS $OVERLAY_DEV + RET=$? + ;; + esac + + [ $RET -ne 0 ] && \ + echo "overlay: format command failed ret: $RET" + + return $RET +} + +check_overlay_partition() +{ + case "$FSTYPE" in + ext4) + fsck.ext4 -y $OVERLAY_DEV + resize2fs $OVERLAY_DEV + sync + ;; + esac +} + +mount_overlay_partition() +{ + mount -t $OVERLAY_FS $OVERLAY_DEV /overlay + RET=$? + + [ $RET -ne 0 ] && \ + echo "overlay: failed to mount $OVERLAY_DEV as $OVERLAY_FS partition ret: $RET" && return $RET + + echo 'overlay: successfully mounted' + + mkdir -p /overlay/upper /overlay/work + mount -o noatime,lowerdir=/,upperdir=/overlay/upper,workdir=/overlay/work -t overlay "overlayfs:/overlay" /mnt \ + || return -1 + + # pivot + mount --move /proc /mnt/proc + + pivot_root /mnt /mnt/rom || return -1 + + mount --move /rom/dev /dev + mount --move /rom/tmp /tmp + mount --move /rom/sys /sys + mount --move /rom/overlay /overlay +} + +do_mount_overlayfs() +{ + [ ! -e /rom/note ] && return + + get_overlay_partition_from_kernel_cmdline || return -1 + + format_overlay_partition || return -1 + + check_overlay_partition + + mount_overlay_partition || return -1 +} + +[ -n "$1" ] || ( do_mount_overlayfs && exit 0 ) diff --git a/target/linux/realtek/image/Makefile b/target/linux/realtek/image/Makefile index b3bee8b4b9..d815f7b48a 100644 --- a/target/linux/realtek/image/Makefile +++ b/target/linux/realtek/image/Makefile @@ -9,7 +9,7 @@ include $(INCLUDE_DIR)/image.mk define Build/mbr dd if=/dev/zero of=$@.mbr bs=512 count=1 >/dev/null - ptgen -v -o $@.mbr -h 16 -s 63 -l 1024 -t 83 -p 131072@131072 -t 83 -p 7340032 + ptgen -v -o $@.mbr -h 16 -s 63 -l 1024 -t 83 -p 131072@131072 -t 83 -p 6291456 endef define Build/install.img @@ -20,8 +20,10 @@ define Build/install.img $(CP) $(IMAGE_KERNEL) $@.install/Image $(CP) $(KDIR)/image-$(firstword $(DEVICE_DTS)).dtb $@.install/rtd-129x.dtb $(CP) $(IMAGE_ROOTFS) $@.install/rootfs.bin + make_ext4fs -J -l 1M -L RTKNAS $@.install/etc.bin - $(TAR) -cf $@.install.img -C $@.install . + rm -f $@.install.img + cd $@.install; $(TAR) -cf $@.install.img `ls` endef define Build/install.img.zip @@ -32,6 +34,7 @@ define Build/install.img.zip $(CP) ./blob/rescue.root.emmc.cpio.gz_pad.img $@.release/ $(CP) $@.install.img $@.release/install.img + rm -f $@ zip -j -X -r $@ $@.release endef diff --git a/target/linux/realtek/image/config/config.txt b/target/linux/realtek/image/config/config.txt index a652a573aa..6540e48ea2 100644 --- a/target/linux/realtek/image/config/config.txt +++ b/target/linux/realtek/image/config/config.txt @@ -22,3 +22,4 @@ fw = linuxKernel Image 0x03000000 ### part = (name mount_point filesystem file size) part = rootfs / squashfs rootfs.bin 134217728 +part = etc etc ext4 etc.bin 6442450944 diff --git a/target/linux/realtek/rtd129x/target.mk b/target/linux/realtek/rtd129x/target.mk index 0199c6452a..af3cc81954 100644 --- a/target/linux/realtek/rtd129x/target.mk +++ b/target/linux/realtek/rtd129x/target.mk @@ -5,7 +5,7 @@ ARCH:=aarch64 BOARDNAME:=RTD129x NAS CPU_TYPE:=cortex-a53 FEATURES+=ext4 -DEFAULT_PACKAGES+=e2fsprogs ethtool mkf2fs partx-utils +DEFAULT_PACKAGES+=rtk-init e2fsprogs resize2fs ethtool mkf2fs partx-utils KERNELNAME:=Image dtbs