Debian 作為全球最流行的 Linux 發行版之一,以其穩定性與自由軟體理念著稱。雖然在國內桌面佔有率不高,但其優秀的硬體相容性使其成為嵌入式裝置(如 ARM 架構機頂盒)的理想選擇。本文詳細記錄如何在全志晶片方案的機頂盒上,從零開始編譯 U-Boot、Linux Kernel,構建 Debian Rootfs,最終實現從 SD 卡啟動完整原生 Debian 系統。

gcc-arm-linux-gnueabihfgit, make, u-boot-tools (mkimage), debootstrap, qemu-user-static, sunxi-tools (bin2fex/fex2bin)script.bin)script.bin 是全志平台的硬體設定二進制檔,定義了 GPIO、DRAM 時序、顯示輸出(HDMI/VGA/解析度)等關鍵參數。必須從原廠韌體中提取並按需修改。
透過 TTL 進入 U-Boot 命令列,或在 Android 系統下以 Root 權限掛載 nanda 分區:
# 在設備端執行
mkdir /sdcard/nanda
mount -t vfat /dev/block/nanda /sdcard/nanda
exit
# 在電腦端透過 ADB 拉取
adb pull /sdcard/nanda/script.bin
# 編譯 sunxi-tools
git clone https://github.com/linux-sunxi/sunxi-tools
cd sunxi-tools && make
# 轉為可編輯的 FEX 文字格式
./bin2fex script.bin script.fex
# 編輯 script.fex,修改 dram_para、hdmi_para、gpio 等節點
vim script.fex
# 重新編譯為二進制
./fex2bin script.fex script.bin
因廠商未釋出原始碼,採用 Cubieboard2 的 U-Boot 源碼(wip/a20 分支)經實測可相容。
git clone https://github.com/linux-sunxi/u-boot-sunxi -b wip/a20
cd u-boot-sunxi
make cubieboard2 ARCH=arm CROSS_COMPILE=arm-linux-gnueabihf-
編譯產出:spl/sunxi-spl.bin (SPL) 和 u-boot.bin。
使用 fdisk 分兩個區:sdb1 (FAT32, ~100MB, 放 Kernel/Script/boot.scr) 和 sdb2 (ext4, 剩餘空間, 放 Rootfs)。
# 寫入 SPL 到第 8KB 偏移
dd if=spl/sunxi-spl.bin of=/dev/sdb bs=1024 seek=8
# 寫入 U-Boot 到第 32KB 偏移
dd if=u-boot.bin of=/dev/sdb bs=1024 seek=32
建立 boot.cmd:
setenv bootargs console=ttyS0,115200 root=/dev/mmcblk0p2 rootwait panic=10 ${extra}
fatload mmc 0 0x48000000 uImage
bootm 0x48000000
編譯為 U-Boot 腳本格式:
mkimage -C none -A arm -T script -d boot.cmd boot.scr
同樣採用 Cubieboard2 的 Kernel 源碼(linux-sunxi),需手動添加機頂盒專用的 PHY 網卡驅動 (ICplus IP101A/G)。
git clone https://github.com/cubieboard2/linux-sunxi
cd linux-sunxi
make ARCH=arm CROSS_COMPILE=arm-linux-gnueabihf- sun7i_defconfig
make ARCH=arm CROSS_COMPILE=arm-linux-gnueabihf- menuconfig
# 在 menuconfig 中啟用:
# Device Drivers -> Network device support -> Ethernet driver support -> ICPlus IP101 support
make -j$(nproc) ARCH=arm CROSS_COMPILE=arm-linux-gnueabihf- uImage modules
make ARCH=arm CROSS_COMPILE=arm-linux-gnueabihf- INSTALL_MOD_PATH=output modules_install
產出檔案:arch/arm/boot/uImage 和 output/lib/ (模組)。
使用 debootstrap 在 x86 主機上透過 QEMU 模擬 ARM 環境構建。
debootstrap --verbose --arch=armhf --foreign jessie debian-rootfs https://ftp.cn.debian.org/debian
cd debian-rootfs
cp /usr/bin/qemu-arm-static usr/bin/
LC_ALL=C LANGUAGE=C LANG=C chroot . /debootstrap/debootstrap --second-stage
LC_ALL=C LANGUAGE=C LANG=C chroot . dpkg --configure -a
chroot .
# 設定 root 密碼
passwd
# 設定主機名與 hosts
echo "a20" > etc/hostname
echo "127.0.0.1 a20" >> etc/hosts
# 設定序列埠終端登入
echo "T0:2345:respawn:/sbin/getty -L ttyS0 115200 vt100" >> etc/inittab
# 設定 APT 源
cat > etc/apt/sources.list < etc/locale.gen
echo "zh_CN.UTF-8 UTF-8" >> etc/locale.gen
locale-gen
# 修改網路設定 (靜態 IP) 及 SSH 允許 Root 登入
vim etc/network/interfaces
vim etc/ssh/sshd_config
exit
# 掛載分區
mkdir -p /mnt/sd1 /mnt/sd2
mount /dev/sdb1 /mnt/sd1
mount /dev/sdb2 /mnt/sd2
# 複製 Kernel、DTB、script.bin、boot.scr 到 FAT 分區
cp arch/arm/boot/uImage /mnt/sd1/
cp script.bin /mnt/sd1/
cp boot.scr /mnt/sd1/
# 複製 Rootfs 到 ext4 分區
cp -a debian-rootfs/* /mnt/sd2/
# 複製 Kernel 模組
cp -a output/lib/* /mnt/sd2/lib/
# 同步並卸載
sync
umount /mnt/sd1 /mnt/sd2
將 SD 卡插入機頂盒上電,透過序列埠 (115200 8N1) 或 SSH 即可登入完整的 Debian 系統。後續可嘗試安裝 xserver-xorg-video-fbturbo 和 lxde 實現桌面環境。
此過程核心在於:提取原廠硬體描述、選擇相容的開源 U-Boot/Kernel 源碼、補齊專屬硬體驅動、構建純淨 Rootfs。雖然機頂盒效能有限,無法跑大型應用,但作為輕量級伺服器、下載機、Home Assistant 中心等用途綽綽有餘。關鍵難點通常在於 DRAM 時序調整與顯示/網路驅動相容,需耐心除錯。