Skip to content

Commit

Permalink
ramips/mt7621: Add HATLab GateBoard-One Support
Browse files Browse the repository at this point in the history
Signed-off-by: Aodzip <aodzip@gmail.com>
Signed-off-by: AnYun <amadeus@openjmu.xyz>
  • Loading branch information
aodzip authored and aiamadeus committed Aug 28, 2022
1 parent ea9e965 commit 3302e26
Show file tree
Hide file tree
Showing 27 changed files with 2,072 additions and 2 deletions.
54 changes: 54 additions & 0 deletions package/system/rdloader/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
include $(TOPDIR)/rules.mk

PKG_NAME:=rdloader
PKG_VERSION:=1.0.1
PKG_RELEASE:=1

PKG_LICENSE:=GPLv3
PKG_BUILD_DEPENDS:=util-linux e2fsprogs

include $(INCLUDE_DIR)/package.mk
include $(INCLUDE_DIR)/cmake.mk

define Package/rdloader
SECTION:=base
CATEGORY:=Base system
TITLE:=kmod loader for ramdisk enviroment
DEPENDS:=@TARGET_ramips +e2fsprogs +libblkid
endef

define Package/rdloader/description
Lightweight kmod loader for ramdisk enviroment,
support uuid-based rootfs mount
endef

define Build/Prepare
mkdir -p $(PKG_BUILD_DIR)
$(CP) ./src/* $(PKG_BUILD_DIR)/
endef

CMAKE_OPTIONS += -DCMAKE_BUILD_TYPE=Release

define Build/InstallDev
rm -fR $(STAGING_DIR)/rdloader
mkdir -p $(STAGING_DIR)/rdloader/{bin,lib,etc}

$(CP) $(PKG_BUILD_DIR)/rdloader $(STAGING_DIR)/rdloader/bin/
$(CP) $(STAGING_DIR_ROOT)/usr/sbin/e2fsck $(STAGING_DIR)/rdloader/bin/
$(CP) $(STAGING_DIR_ROOT)/etc/e2fsck.conf $(STAGING_DIR)/rdloader/etc/

( \
export \
READELF=$(TARGET_CROSS)readelf \
OBJCOPY=$(TARGET_CROSS)objcopy \
XARGS="$(XARGS)"; \
find $(STAGING_DIR)/rdloader/bin/ -type f -a -exec \
$(SCRIPT_DIR)/gen-rddependencies.sh {} \; ; \
) | while read FILE; do \
cp $(STAGING_DIR_ROOT)/lib/$$$$FILE $(STAGING_DIR)/rdloader/lib/ || \
cp $(STAGING_DIR_ROOT)/usr/lib/$$$$FILE $(STAGING_DIR)/rdloader/lib/ \
; \
done;
endef

$(eval $(call BuildPackage,rdloader))
9 changes: 9 additions & 0 deletions package/system/rdloader/src/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
cmake_minimum_required(VERSION 3.15)
set(CMAKE_C_STANDARD 99)
project(rdloader C)

add_executable(rdloader main.c insmod.c insmod.h mknod.c mknod.h mkdev_node.c mkdev_node.h blkid2.c blkid2.h cmdline.c cmdline.h switch_root.c switch_root.h)

target_link_libraries(${PROJECT_NAME} blkid)

install(TARGETS rdloader RUNTIME DESTINATION /usr/sbin)
40 changes: 40 additions & 0 deletions package/system/rdloader/src/blkid2.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
//
// Created by juno on 2022/1/11.
//

#include <stdio.h>
#include <blkid/blkid.h>
#include <errno.h>
#include <stdlib.h>
#include <string.h>
#include "blkid2.h"

int get_blkid(const char *blkdev, char **uuid)
{
blkid_probe pr;
int ret;
size_t len;
const char *data;
void *out;

pr = blkid_new_probe_from_filename(blkdev);
if (!pr)
{
fprintf(stderr, "Failed open %s:%s\n", blkdev, strerror(errno));
return -1;
}

blkid_do_probe(pr);
ret = blkid_probe_lookup_value(pr, "UUID", &data, &len);

if (ret == 0)
{
out = calloc(1, len + 1);
memcpy(out, data, len);
*uuid = out;
}

blkid_free_probe(pr);

return ret;
}
10 changes: 10 additions & 0 deletions package/system/rdloader/src/blkid2.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
//
// Created by juno on 2022/1/11.
//

#ifndef INIT_BLKID2_H
#define INIT_BLKID2_H

int get_blkid(const char *blkdev, char **uuid);

#endif //INIT_BLKID2_H
209 changes: 209 additions & 0 deletions package/system/rdloader/src/cmdline.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,209 @@
//
// Created by juno on 2022/1/11.
//

#define _GNU_SOURCE
#include <stdio.h>
#include <errno.h>
#include <unistd.h>
#include <stdlib.h>
#include <string.h>

#include "cmdline.h"

static void insert_fstype_tail(struct fstype **head, struct fstype *fs)
{
struct fstype *curr = *head;
struct fstype *next = NULL;

if (!curr)
{
*head = fs;
return;
}

do
{
next = curr->next;
if (!next)
{
curr->next = fs;
break;
}
curr = next;
} while (curr);
}

static int parse_root(char *args, char **ret)
{
char *root = NULL;

if (!args)
return -1;

if (strstr(args, "PARTUUID=") != NULL)
{
root = strtok(args, "=");
root = strtok(NULL, "=");
root = strtok(NULL, "=");
if (!root)
return -1;

asprintf(ret, "/dev/disk/by-partuuid/%s", root);
}
else if (strstr(args, "UUID=") != NULL)
{

root = strtok(args, "=");
root = strtok(NULL, "=");
root = strtok(NULL, "=");

if (!root)
return -1;

asprintf(ret, "/dev/disk/by-uuid/%s", root);
}
else if (strstr(args, "/dev/"))
{
*ret = strdup(args);
}
else
{
asprintf(ret, "/dev/%s", args);
}

return 0;
}

struct fstype *parse_rootfs(char **fstype_list)
{
struct fstype *ret = NULL;
char *list = *fstype_list;
struct fstype *default_fs = NULL;
struct fstype *fs = NULL;

if (list)
{
list = strtok(list, "=");

while ((list = strtok(NULL, ",")))
{
fs = calloc(1, sizeof(struct fstype));

if (!fs)
return NULL;

fs->name = strdup(list);
insert_fstype_tail(&ret, fs);
}
}

default_fs = calloc(1, sizeof(struct fstype));
default_fs->name = strdup("ext4");
insert_fstype_tail(&ret, default_fs);

return ret;
}

int parse_cmdline(struct bootargs_t **ret)
{
FILE *fp;
char *cmdline = NULL;
struct fstype *fs = NULL;
char str[1024] = {0};
char *root = NULL;
char *rootfs = NULL;
struct bootargs_t *bootargs = NULL;

fp = fopen("/proc/cmdline", "r");

if (!fp)
{
fprintf(stderr, "get cmdline failed:%s\n", strerror(errno));
return -1;
}

if (fgets(str, sizeof(str) - 1, fp) == NULL)
{
fprintf(stderr, "cmdline empty\n");
fclose(fp);
return -1;
}

fclose(fp);

cmdline = strtok(str, " ");

do
{
if (!cmdline)
continue;

if (strstr(cmdline, "root="))
root = cmdline;
if (strstr(cmdline, "rootfstype="))
rootfs = cmdline;

} while ((cmdline = strtok(NULL, " ")) != NULL);

if (parse_root(root, &root) < 0)
{
fprintf(stderr, "root args error\n");
return -1;
}

if ((fs = parse_rootfs(&rootfs)) == NULL)
{
fprintf(stderr, "fstype error\n");
return -1;
}

bootargs = calloc(1, sizeof(struct bootargs_t));

if (!bootargs)
{
fprintf(stderr, "alloc bootargs failed\n");
free(fs);
free(root);
return -1;
}

bootargs->root = root;
bootargs->fstype = fs;

*ret = bootargs;

return 0;
}

void free_bootargs(struct bootargs_t **args)
{
struct bootargs_t *bootargs = *args;
struct fstype *curr = NULL;
struct fstype *next = NULL;

if (!bootargs)
return;

if (bootargs->root)
free(bootargs->root);

curr = bootargs->fstype;

do
{
if (curr)
{
if (curr->name)
free(curr->name);

next = curr->next;
free(curr);
}

curr = next;

} while (curr);

free(bootargs);
}
24 changes: 24 additions & 0 deletions package/system/rdloader/src/cmdline.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
//
// Created by juno on 2022/1/11.
//

#ifndef INIT_CMDLINE_H
#define INIT_CMDLINE_H

struct fstype
{
char *name;
struct fstype *next;
};

struct bootargs_t
{
char *root;
struct fstype *fstype;
};

int parse_cmdline(struct bootargs_t **ret);

void free_bootargs(struct bootargs_t **args);

#endif //INIT_CMDLINE_H
Loading

0 comments on commit 3302e26

Please sign in to comment.