Compare commits

..

10 Commits

Author SHA1 Message Date
Takumi Sueda
2e954a9b4b Merge pull request #61 from tslmy/buildroot
Add Docker-based macOS build workflow for the Buildroot variant
2026-06-24 16:37:18 +09:00
Mingyang Li
faa1a37ef1 Update buildroot reference. 2026-06-19 09:55:18 -07:00
Mingyang Li
7787b5fc9f The new Buildroot output folder should be ignored by git. 2026-06-19 09:50:36 -07:00
Mingyang Li
85cd5a7e1e fix admonition 2026-06-17 21:30:55 -07:00
Mingyang Li
41b199cc9f (Docker-based) Extend image-patching targets to BOTH rootfs variants 2026-06-17 21:23:21 -07:00
Ming
7c012f9ff6 Apply suggestion from @puhitaku
Co-authored-by: Takumi Sueda <puhitaku@gmail.com>
2026-06-17 20:51:09 -07:00
Mingyang Li
5c532c865d + Docker-based makefile targets for Buildroot 2026-06-10 21:00:15 -07:00
Mingyang Li
07fdf6c0d8 build_image*.sh: Copy with /. (directory-contents idiom) instead of /* (glob)
so that hidden files are included and the copy succeeds even on an edge-case empty dir.
2026-06-10 21:00:15 -07:00
Takumi Sueda
e28927cf02 Merge pull request #60 from tslmy/chore/docker-build-macos-workflow
Add Docker-based macOS build workflow and stabilize SD image assembly
2026-06-11 05:19:46 +09:00
Mingyang Li
bf54dfbf44 Address review comments from puhitaku
- image/build_image.sh: fix IMG_NAME default to sd.img, SIZE_M to 3072
- image/build_image.sh: revert START2 to original form; remove all unnecessary inline comments
- Makefile: remove 'Mount proc and sys' comment; keep the 'Keep mounting commands AFTER' note
- Makefile: remove 'Copy qemu-arm-static' comment; simplify binfmt and mmap_min_addr comments
- Makefile: replace verbose Docker target comment blocks with clean targets per reviewer suggestion
- README.md: revert all unrelated changes; keep only the macOS environment line and Docker build section

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
2026-06-06 10:33:00 -07:00
8 changed files with 288 additions and 93 deletions

1
.gitignore vendored
View File

@@ -6,6 +6,7 @@ nk.bin
image/sd*.img
*.exe
image/work
buildroot_output
# Created by https://www.toptal.com/developers/gitignore/api/macos,linux,windows
# Edit at https://www.toptal.com/developers/gitignore?templates=macos,linux,windows

36
Dockerfile.buildroot Normal file
View File

@@ -0,0 +1,36 @@
ARG BUILDROOT_PLATFORM=linux/amd64
# TODO: Update to Trixie once we catch up with upstream Buildroot.
FROM --platform=${BUILDROOT_PLATFORM} debian:bookworm
ENV DEBIAN_FRONTEND=noninteractive
RUN apt-get update \
&& apt-get install -y --no-install-recommends \
bc \
bison \
build-essential \
bzip2 \
ca-certificates \
cmake \
cpio \
diffutils \
file \
flex \
gawk \
git \
libncurses-dev \
libssl-dev \
make \
patch \
perl \
python3 \
python3-dev \
rsync \
unzip \
wget \
xz-utils \
&& rm -rf /var/lib/apt/lists/*
WORKDIR /work
CMD ["bash"]

143
Makefile
View File

@@ -7,6 +7,11 @@ export ARCH=arm
DOCKER_IMAGE := buildbrain-builder:local
ROOTFS_VOLUME := buildbrain-brainux-rootfs
# Separate, leaner image for Buildroot. Based on debian:bookworm (GCC 12)
# which is compatible with the 2023.05-era Buildroot fork without any patches.
BUILDROOT_DOCKER_IMAGE := buildbrain-buildroot:local
BUILDROOT_VOLUME := buildbrain-buildroot-rootfs
BUILDROOT_OUTPUT_VOLUME := buildbrain-buildroot-output
.PHONY:
setup:
@@ -138,7 +143,7 @@ lilobuild:
liloclean:
make -C ./brainlilo clean
.PHONY: brainux brainux-umount-special brainux-clean
.PHONY: brainux brainux-umount-special brainux-clean buildroot_rootfs
brainux:
@if [ "$(shell uname)" != "Linux" ]; then \
echo "Debootstrap is only available in Linux!"; \
@@ -152,7 +157,6 @@ brainux:
sudo debootstrap --arch=$(ROOTFS_CROSS) --foreign trixie brainux/ http://localhost:65432/debian/; \
fi
# Mount proc and sys to allow debootstrap to run the second stage in the chroot.
# Keep the mounting commands AFTER the first stage of debootstrap, because
# debootstrap's cleanup code/trap tries to clean up the target directory
# (`rm -rf /work/brainux/proc`) and fails because proc virtual files can't be removed.
@@ -160,15 +164,10 @@ brainux:
sudo mount -t proc none $(shell pwd)/brainux/proc
sudo mount --rbind /sys $(shell pwd)/brainux/sys
# Copy qemu-arm-static and setup script to allow running the second stage of
# debootstrap in the chroot on an x86 host.
sudo cp /usr/bin/qemu-arm-static brainux/usr/bin/
sudo cp ./os-brainux/setup_brainux.sh brainux/
sudo ./os-brainux/override-pre.sh ./os-brainux/override ./brainux
# Register qemu-arm-static binfmt handler if not already present.
# The F (fixed) flag makes the kernel resolve the interpreter from the
# host filesystem so it works inside the chroot even without qemu in it.
# This is a no-op if the entry already exists (e.g. in CI or native Linux).
sudo bash -c 'mount binfmt_misc -t binfmt_misc /proc/sys/fs/binfmt_misc 2>/dev/null; test -e /proc/sys/fs/binfmt_misc/qemu-arm || echo ":qemu-arm:M::\x7fELF\x01\x01\x01\x00\x00\x00\x00\x00\x00\x00\x00\x00\x02\x00\x28\x00:\xff\xff\xff\xff\xff\xff\xff\x00\xff\xff\xff\xff\xff\xff\xff\xff\xfe\xff\xff\xff:/usr/bin/qemu-arm-static:F" > /proc/sys/fs/binfmt_misc/register'
# Allow qemu-arm-static to reserve the guest address space at low virtual
# addresses (0x1000). On Linux hosts vm.mmap_min_addr defaults to 65536
@@ -188,10 +187,10 @@ brainux-clean: brainux-umount-special
sudo rm -rf brainux
buildroot_rootfs:
make -C buildroot brain_imx28_defconfig
make -C buildroot -j 12
sudo mkdir -p buildroot_rootfs
sudo tar -C ./buildroot_rootfs -xf buildroot/output/images/rootfs.tar
make -C buildroot O=/work/buildroot_output brain_imx28_defconfig
make -C buildroot O=/work/buildroot_output
mkdir -p buildroot_rootfs
tar -C ./buildroot_rootfs -xf buildroot_output/images/rootfs.tar
image/sd.img: clean_work
./image/build_image.sh brainux sd.img 3072
@@ -217,33 +216,24 @@ aptcache:
datetag:
git tag $(shell ./tools/version)
# ========== Docker-based build targets (for macOS and other non-Linux hosts) ==========
.PHONY:
docker-build:
docker build --platform linux/amd64 -t $(DOCKER_IMAGE) -f Dockerfile .
.PHONY:
docker-buildroot-build:
docker build --platform linux/amd64 -t $(BUILDROOT_DOCKER_IMAGE) -f Dockerfile.buildroot .
.PHONY:
docker-uboot:
docker run --rm --platform linux/amd64 -v "$$PWD":/work -w /work $(DOCKER_IMAGE) \
bash -lc "make udefconfig-sh1 && make ubuild"
# Build Linux kernel using brain defconfig.
# mrproper wipes stale host-tool binaries (e.g. arm64 objects left from a
# previous native build) so they are always recompiled for the container's
# architecture before defconfig and the full build run.
.PHONY:
docker-kernel:
docker run --rm --platform linux/amd64 -v "$$PWD":/work -w /work $(DOCKER_IMAGE) \
bash -lc "make lclean; make ldefconfig && make lbuild"
# Build Debian rootfs in container with debootstrap and qemu.
# The rootfs is stored in a Docker named volume (Linux ext4 inside the Docker
# Desktop VM) instead of the macOS APFS bind mount. This is critical: APFS
# cannot represent mknod device files or preserve all Linux permission bits,
# which produces a rootfs that fails to boot despite appearing structurally
# complete. A named volume stores a true Linux filesystem and avoids all of
# these issues.
.PHONY:
docker-rootfs: docker-volume-rm docker-volume-create
docker run --rm --platform linux/amd64 --privileged -e CI=true \
@@ -251,10 +241,6 @@ docker-rootfs: docker-volume-rm docker-volume-create
-v "$$PWD":/work -w /work $(DOCKER_IMAGE) \
bash -lc "make brainux"
# Assemble SD image from pre-built kernel and rootfs.
# Requires privileged mode because make targets use loop devices, kpartx and mount.
# Mounts the same named volume used by docker-rootfs so the rootfs copy into the
# ext4 partition originates from the Linux-native volume, not from macOS APFS.
.PHONY:
docker-sd-image:
docker run --rm --platform linux/amd64 --privileged \
@@ -262,14 +248,9 @@ docker-sd-image:
-v "$$PWD":/work -w /work $(DOCKER_IMAGE) \
bash -lc "make -C nkbin_maker clean all && make IMG_BUILD_JOBS=1 image/sd.img"
# Build complete SD image from scratch (stages: kernel, rootfs, then assembly).
# We split the build into 3 phases to avoid overwhelming the daemon on macOS Docker Desktop.
.PHONY:
docker-sd-image-full: docker-kernel docker-rootfs docker-sd-image
# --------------------- Docker named-volume helpers ---------------------
# docker-rootfs already recreates the volume automatically; these targets are
# provided for manual use (e.g. inspecting, wiping, or recreating between runs).
.PHONY:
docker-volume-create:
docker volume create $(ROOTFS_VOLUME)
@@ -278,4 +259,98 @@ docker-volume-create:
docker-volume-rm:
docker volume rm $(ROOTFS_VOLUME) 2>/dev/null || true
# ==================== end of Docker-based build targets ====================
.PHONY:
docker-buildroot-rootfs: docker-buildroot-volume-rm docker-buildroot-volume-create docker-buildroot-output-volume-create
docker run --rm --platform linux/amd64 --privileged \
-v $(BUILDROOT_OUTPUT_VOLUME):/work/buildroot_output \
-v $(BUILDROOT_VOLUME):/work/buildroot_rootfs \
-v "$$PWD":/work -w /work $(BUILDROOT_DOCKER_IMAGE) \
bash -lc "make buildroot_rootfs"
.PHONY:
docker-buildroot-menuconfig:
docker run --rm -it --platform linux/amd64 \
-v "$$PWD":/work -w /work $(BUILDROOT_DOCKER_IMAGE) \
bash -lc "rm -rf buildroot/output/build/buildroot-config && make -C buildroot brain_imx28_defconfig && make -C buildroot menuconfig"
# Run after docker-buildroot-menuconfig to persist customisations.
.PHONY:
docker-buildroot-savedefconfig:
docker run --rm --platform linux/amd64 \
-v "$$PWD":/work -w /work $(BUILDROOT_DOCKER_IMAGE) \
bash -lc "rm -rf buildroot/output/build/buildroot-config && make -C buildroot savedefconfig BR2_DEFCONFIG=configs/brain_imx28_defconfig"
.PHONY:
docker-buildroot-sd-image:
docker run --rm --platform linux/amd64 --privileged \
-v $(BUILDROOT_VOLUME):/work/buildroot_rootfs \
-v "$$PWD":/work -w /work $(DOCKER_IMAGE) \
bash -lc "make -C nkbin_maker clean all && make IMG_BUILD_JOBS=1 image/sd_buildroot.img"
.PHONY:
docker-buildroot-full: docker-kernel docker-buildroot-rootfs docker-buildroot-sd-image
.PHONY:
docker-buildroot-volume-create:
docker volume create $(BUILDROOT_VOLUME)
.PHONY:
docker-buildroot-volume-rm:
docker volume rm $(BUILDROOT_VOLUME) 2>/dev/null || true
.PHONY:
docker-buildroot-output-volume-create:
docker volume create $(BUILDROOT_OUTPUT_VOLUME)
.PHONY:
docker-buildroot-output-volume-rm:
docker volume rm $(BUILDROOT_OUTPUT_VOLUME) 2>/dev/null || true
# ------------ Fast partition-only updates ------------
# Requires image/sd(_buildroot).img to already exist.
# Fast rootfs-only update: replace only the ext4 partition (p2) in an existing
# sd(_buildroot).img without rebuilding U-Boot (saves ~35 min per iteration).
# Requires image/sd(_buildroot).img to already exist from a prior full build.
#
# Workflow for rootfs-only changes:
# 1. Edit files under os-(buildroot|brainux)/override/
# or (in the case of Buildroot) re-configure
# 2. make docker-(buildroot-)rootfs (~1 min)
# 3. make docker-(buildroot-)patch-rootfs-image (~1 min)
# 4. Flash image/sd(_buildroot).img
.PHONY:
docker-patch-rootfs-image:
docker run --rm --platform linux/amd64 --privileged \
-v $(ROOTFS_VOLUME):/work/brainux \
-v "$$PWD":/work -w /work $(DOCKER_IMAGE) \
./tools/patch_image.sh rootfs-brainux image/sd.img
.PHONY:
docker-buildroot-patch-rootfs-image:
docker run --rm --platform linux/amd64 --privileged \
-v $(BUILDROOT_VOLUME):/work/buildroot_rootfs \
-v "$$PWD":/work -w /work $(DOCKER_IMAGE) \
./tools/patch_image.sh rootfs-buildroot image/sd_buildroot.img
# Fast kernel-only update: replace only boot partition (p1) kernel artifacts
# in an existing sd(_buildroot).img, w/o rebuilding rootfs or repacking image.
#
# Workflow for kernel-only changes:
# 1. Edit files under `linux-brain/`
# 2. make docker-kernel
# 3. make docker-(buildroot-)patch-kernel-image
# 4. Flash image/sd(_buildroot).img
.PHONY:
docker-patch-kernel-image:
docker run --rm --platform linux/amd64 --privileged \
-v "$$PWD":/work -w /work $(DOCKER_IMAGE) \
./tools/patch_image.sh kernel image/sd.img
.PHONY:
docker-buildroot-patch-kernel-image:
docker run --rm --platform linux/amd64 --privileged \
-v "$$PWD":/work -w /work $(DOCKER_IMAGE) \
./tools/patch_image.sh kernel image/sd_buildroot.img

View File

@@ -1,13 +1,12 @@
buildbrain
==========
Scripts for building [Brainux](https://brainux.org/), a custom Linux distribution for the [Sharp Brain](https://jp.sharp/edictionary/) series of electronic dictionaries.
This repository includes:
This meta-repository includes:
- linux-brain, u-boot-brain, nkbin_maker and boot4u as submodules
- Useful build targets in Makefile
- r3build.toml to watch changes that occur in submodules
- linux-brain, u-boot-brain, nkbin_maker and boot4u as submodules
- Useful build targets in Makefile
- `r3build.toml` to watch changes that occur in submodules
Confirmed environments
----------------------
@@ -16,40 +15,36 @@ Confirmed environments
- Debian 11 (bullseye) amd64
- macOS 26.5 (Tahoe) arm64-apple-darwin25.5.0 via Docker
**Typical Runtime**: 3 hrs is typical on a M2 Max MacBook Pro via Docker.
Getting Started
---------------
For Debian-based systems:
1. Install dependencies.
```sh
sudo apt install build-essential bison flex libncurses5-dev gcc-arm-linux-gnueabi gcc-arm-linux-gnueabihf libssl-dev bc lzop qemu-user-static debootstrap kpartx libyaml-dev python3-pyelftools
```
$ sudo apt install build-essential bison flex libncurses5-dev gcc-arm-linux-gnueabi gcc-arm-linux-gnueabihf libssl-dev bc lzop qemu-user-static debootstrap kpartx libyaml-dev python3-pyelftools
```
2. Clone this repository recursively.
1. Clone this repository with recursive clone enabled.
```sh
git clone --recursive git@github.com:brain-hackers/buildbrain.git
```
$ git clone --recursive git@github.com:brain-hackers/buildbrain.git
```
- If you've cloned it without `--recursive`, run following command:
```sh
git submodule update --init --recursive
```
$ git submodule update --init --recursive
```
3. Install `uuu`.
1. Install uuu.
- Follow [the instruction](https://github.com/NXPmicro/mfgtools#linux) and build `uuu` executable.
- Put `uuu` where the PATH executable points to.
For macOS, see [Docker build](#docker-build) section below.
Build U-Boot
------------
-----------------------
1. Run `make udefconfig-sh*` to generate `.config`.
@@ -61,9 +56,9 @@ Build U-Boot
- i.MX283 loads a packed U-Boot executable called `u-boot.sb`.
Inject U-Boot into i.MX283 in recovery mode
-----------------------
1. Follow `Build U-Boot` procedure to make U-Boot binary.
1. Run `make uuu`
@@ -89,6 +84,7 @@ Build and deploy boot4u
- `touch /path/to/your/sd/1st/partition/App/boot4u/index.din`
- `cp boot4u/AppMain.bin /path/to/your/sd/1st/partition/App/boot4u/`
Build Linux
-----------
@@ -98,6 +94,7 @@ Build Linux
1. Confirm that `linux-brain/arch/arm/boot/zImage` exists.
Build a Debian rootfs
---------------------
@@ -111,6 +108,7 @@ Build a Debian rootfs
1. Confirm that `image/sd.img` is built and burn it to an SD card.
Build a Buildroot rootfs
------------------------
@@ -118,11 +116,18 @@ Buildroot rootfs aims to be the most lightweight rootfs for experimental use. `m
If you want to customize the build of Buildroot, `cd` into `buildroot` and use the following targets:
- `make menuconfig` to change the configuration
- `make` to build the rootfs (`-j` option might give you extra speed)
- `make menuconfig` to change the configuration
- `make` to build the rootfs (`-j` option might give you extra speed)
For Docker-based customization, use the interactive targets:
1. `make docker-buildroot-menuconfig`
2. `make docker-buildroot-savedefconfig` — writes `buildroot/.config` back to `buildroot/configs/brain_imx28_defconfig` so your changes are committed-safe.
3. `make docker-buildroot-rootfs` — rebuilds the rootfs with the updated config.
`image/sd_buildroot.img` target expects presence of the tarball at `buildroot/output/images/rootfs.tar`. You'll have to `clean` and rebuild every time you change the Buildroot's config before making the SD image.
Docker build
------------
@@ -159,11 +164,29 @@ You can build everything in Docker instead of preparing native Linux cross toolc
**Note:** `make docker-rootfs` (and thus `make docker-sd-image-full`) always deletes and recreates the named volume `buildbrain-brainux-rootfs` before building, so each rootfs build starts from a clean slate. To delete the volume manually between runs use `make docker-volume-rm`.
3. *(Optional)* Build a Buildroot-based SD image instead.
```sh
make docker-buildroot-full
```
This builds the Linux kernel, the Buildroot rootfs, and assembles `image/sd_buildroot.img`. Run each stage independently if preferred:
```sh
make docker-kernel
make docker-buildroot-rootfs
make docker-buildroot-sd-image
```
The Buildroot rootfs is stored in a separate named volume (`buildbrain-buildroot-rootfs`) for the same Linux-filesystem reasons as the Debian rootfs. `make docker-buildroot-rootfs` always recreates it from scratch; use `make docker-buildroot-volume-rm` to wipe it manually.
### Direct Docker commands (advanced)
For macOS, run in **stages** and use a **named volume** for the rootfs.
> [!NOTE] Why a named volume for the rootfs?
> [!NOTE]
> **Why a named volume for the rootfs?**
>
> macOS APFS (the host filesystem behind Docker bind mounts) cannot create device
> files (`mknod`), may strip `setuid` bits, and does not faithfully preserve all
> Linux filesystem attributes. If the Debian rootfs is stored on APFS the result
@@ -204,12 +227,11 @@ Other useful Docker recipes:
- `make docker-uboot` to build U-Boot
- `make docker-kernel` to build Linux kernel
- `make docker-volume-create` to (re-)create the rootfs named volume
- `make docker-volume-rm` to delete the rootfs named volume and reclaim its disk space
- `make docker-(buildroot-)volume-(create|rm)` to manage the Debian/Buildroot rootfs volume
- `make docker-(buildroot-)patch-(kernel|rootfs)-image` to quickly update just one partition in `image/sd(_buildroot).img` (which must already exist)
Known issues
------------
----------------------------------------
If you use GCC 10 for the host compiler, `make ubuild` may fail.
To complete build, open `/u-boot-brain/scripts/dtc/dtc-lexer.lex.c` or `/u-boot-brain/scripts/dtc/dtc-parser.tab.c` then comment out `YYLTYPE yylloc;`
@@ -221,12 +243,10 @@ Watch changes in submodules & auto-build
- Python 3 venv in `env`
- r3build command in the env
2. Run `r3build`. It'll detect the changes you make and build the corresponding executable automatically.
1. Run `r3build`. It'll detect the changes you make and builds the corresponding executable automatically.
> [!NOTE] What's r3build?
> [r3build](https://github.com/puhitaku/r3build) is a smart file watcher that aims to provide hot-reloading feature like Web frontend development.
Disclaimer
----------
What's r3build?
---------------
This repository is not affiliated with Sharp Corporation. The content is provided "as is" without any warranties. Use at your own risk.
[r3build](https://github.com/puhitaku/r3build) is a smart file watcher that aims to provide hot-reloading feature like Web frontend development.

View File

@@ -9,8 +9,8 @@ Build a bootable image for Brainux.
Arguments:
ROOTFS Path to the root filesystem directory to include in the image (default: "rootfs").
IMG_NAME Name of the output image file (default: brainux.img).
SIZE_M Size of the output image in megabytes (default: 1024).
IMG_NAME Name of the output image file (default: sd.img).
SIZE_M Size of the output image in megabytes (default: 3072).
EOF
}
@@ -20,18 +20,14 @@ if [[ "$1" == "-h" || "$1" == "--help" || -z "$1" ]]; then
exit 0
fi
# JOBS is used to control the number of parallel jobs when building u-boot.
# By default, it uses the number of CPU cores. This can be overridden by
# setting the IMG_BUILD_JOBS environment variable before running the script,
# handy for Docker environments or when you want to limit resource usage.
JOBS=${IMG_BUILD_JOBS:-$(nproc)}
REPO=$(git rev-parse --show-toplevel)
WORK=${REPO}/image/work
LINUX=${REPO}/linux-brain
ROOTFS=${1:-rootfs} # Default to "rootfs" if not specified
IMG_NAME=${2:-brainux.img}
ROOTFS=${1:-rootfs}
IMG_NAME=${2:-sd.img}
IMG=${REPO}/image/${IMG_NAME}
SIZE_M=${3:-1024} # Default to 1GB if not specified
SIZE_M=${3:-3072}
export CROSS_COMPILE=arm-linux-gnueabi-
mkdir -p ${WORK}
@@ -41,7 +37,6 @@ for i in "a7200" "a7400" "sh1" "sh2" "sh3" "sh4" "sh5" "sh6" "sh7"; do
NUM=$(echo $i | sed -E 's/sh//g')
BUILD_DIR=${WORK}/uboot-build-${i}
# Build per-board from isolated source copies to avoid fragile clean rules.
rm -rf ${BUILD_DIR}
rsync -a --exclude '.git' ${REPO}/u-boot-brain/ ${BUILD_DIR}/
make -C ${BUILD_DIR} pw${i}_defconfig
@@ -66,17 +61,12 @@ for i in "a7200" "a7400" "sh1" "sh2" "sh3" "sh4" "sh5" "sh6" "sh7"; do
esac
done
# Create an empty image file of the specified size.
dd if=/dev/zero of=${IMG} bs=1M count=${SIZE_M}
# We want to partition the image with two partitions:
# * a 64MB FAT32 partition for the bootloader and kernel, and
# * the rest of the space as an ext4 partition for the root filesystem.
START1=2048 # Start at 1MB (2048 sectors of 512 bytes) to align with typical partitioning schemes and avoid issues with some bootloaders.
SECTORS1=$((1024 * 1024 * 64 / 512)) # 64MB in sectors (512 bytes per sector)
START2=$((START1 + SECTORS1)) # Start the second partition immediately after the first
START1=2048
SECTORS1=$((1024 * 1024 * 64 / 512))
START2=$((2048 + ${SECTORS1}))
# We use sfdisk to create the partition table. The first partition is type 'b' (W95 FAT32), and the second is type '83' (Linux).
cat <<EOF > ${WORK}/part.sfdisk
${IMG}1 : start=${START1}, size=${SECTORS1}, type=b
${IMG}2 : start=${START2}, type=83
@@ -84,11 +74,7 @@ EOF
sfdisk ${IMG} < ${WORK}/part.sfdisk
# We want to format and write to the partitions. To do so, we use kpartx to create device mappings for the partitions in the image.
KPARTX_OUTPUT=$(sudo kpartx -av ${IMG})
# Extract the loop device name from kpartx output. It looks like:
# "add map loop0p1 (252:0): 0 131072 linear /dev/loop0 2048"
# We want to extract "loop0" from this line.
LOOPDEV=$(echo "${KPARTX_OUTPUT}" | sed -n 's/^add map \(loop[0-9]\+\)p1.*/\1/p' | head -n 1)
sudo mkfs.fat -n boot -F32 -v -I /dev/mapper/${LOOPDEV}p1
@@ -105,7 +91,6 @@ sudo cp ${LINUX}/arch/arm/boot/dts/imx28-pw*.dtb ${WORK}/p1/
sudo mkdir -p ${WORK}/p1/nk
sudo cp ${WORK}/*.bin ${WORK}/p1/nk/
# Prepare the WinCE application "Launch Linux". "LILO" stands for "Linux Loader".
make -C ${REPO}/brainlilo
LILO="${WORK}/p1/アプリ/Launch Linux"
@@ -120,9 +105,8 @@ sudo cp ${REPO}/image/exeopener.exe "${LILO}/AppMain.exe"
sudo mkdir -p ${WORK}/p1/loader
sudo cp ${WORK}/lilobin/*.bin ${WORK}/p1/loader/
sudo cp -ra ${REPO}/${ROOTFS}/* ${WORK}/p2/
sudo cp -a "${REPO}/${ROOTFS}/." "${WORK}/p2/"
# Clean up: unmount the partitions and remove the device mappings.
sudo umount ${WORK}/p1 ${WORK}/p2
sudo kpartx -d ${IMG}

View File

@@ -43,7 +43,7 @@ sudo mkdir -p ${WORK}/p1/App/boot4u
sudo cp ${REPO}/boot4u/AppMain.bin ${WORK}/p1/App/boot4u/
sudo touch ${WORK}/p1/App/boot4u/index.din
sudo cp -ra ${REPO}/brainux/* ${WORK}/p2/
sudo cp -a "${REPO}/brainux/." "${WORK}/p2/"
sudo umount ${WORK}/p1 ${WORK}/p2
sudo kpartx -d ${IMG}

79
tools/patch_image.sh Executable file
View File

@@ -0,0 +1,79 @@
#!/usr/bin/env bash
set -e
TYPE="$1"
SPECIFIED_IMAGE="$2"
# Parse modes and assign configurations
case "$TYPE" in
"rootfs-buildroot")
IMAGE="${SPECIFIED_IMAGE:-image/sd_buildroot.img}"
PART="2"
MNT_DIR="/mnt/brainp2"
MOUNT_OPTS=""
SRC_DIR="buildroot_rootfs"
;;
"rootfs-brainux")
IMAGE="${SPECIFIED_IMAGE:-image/sd.img}"
PART="2"
MNT_DIR="/mnt/brainp2"
MOUNT_OPTS=""
SRC_DIR="brainux"
;;
"kernel")
IMAGE="${SPECIFIED_IMAGE:-image/sd.img}"
PART="1"
MNT_DIR="/mnt/brainp1"
MOUNT_OPTS="-o utf8=true"
;;
*)
echo "Usage: $0 [kernel|rootfs-buildroot|rootfs-brainux] [optional_image_path]"
exit 1
;;
esac
if [ ! -f "$IMAGE" ]; then
echo "Error: Target image '$IMAGE' does not exist."
exit 1
fi
echo "Mapping $IMAGE with kpartx..."
KPARTX_OUTPUT=$(kpartx -av "$IMAGE")
echo "$KPARTX_OUTPUT"
# Extract loop device name (e.g., loop0)
LONAME=$(echo "$KPARTX_OUTPUT" | sed -n "s/^add map \(loop[0-9]\+\)p${PART}.*/\1/p" | head -1)
if [ -z "$LONAME" ]; then
echo "Failed to detect loop device from kpartx output."
exit 1
fi
MAPPER_DEV="/dev/mapper/${LONAME}p${PART}"
mkdir -p "$MNT_DIR"
# Cleanup trap to ensure we clean up loop devices and mounts on failure
cleanup() {
echo "Cleaning up mounts and loop devices..."
if mountpoint -q "$MNT_DIR"; then
umount "$MNT_DIR"
fi
kpartx -d "$IMAGE" || true
}
trap cleanup EXIT
echo "Mounting partition p${PART} to $MNT_DIR..."
mount $MOUNT_OPTS "$MAPPER_DEV" "$MNT_DIR"
if [[ "$TYPE" == rootfs-* ]]; then
echo "Wiping old rootfs..."
rm -rf "${MNT_DIR:?}"/*
echo "Copying new rootfs from $SRC_DIR..."
cp -a "$SRC_DIR"/. "$MNT_DIR/"
elif [ "$TYPE" = "kernel" ]; then
echo "Updating kernel artifacts on boot partition..."
cp -f linux-brain/arch/arm/boot/zImage "$MNT_DIR/"
cp -f linux-brain/arch/arm/boot/dts/imx28-pw*.dtb "$MNT_DIR/"
fi
sync
echo "Done. ${TYPE} update applied successfully to ${IMAGE}."