build.c 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676
  1. //
  2. // Copyright 2026 Aarav Ravindra Kharade
  3. //
  4. // Licensed under the Apache License, Version 2.0 (the "License");
  5. // you may not use this file except in compliance with the License.
  6. // You may obtain a copy of the License at
  7. //
  8. // http://www.apache.org/licenses/LICENSE-2.0
  9. //
  10. // Unless required by applicable law or agreed to in writing, software
  11. // distributed under the License is distributed on an "AS IS" BASIS,
  12. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. // See the License for the specific language governing permissions and
  14. // limitations under the License.
  15. //
  16. /*
  17. * ArkOS Build System v3.0
  18. * -----------------------
  19. * Orchestrates the complete OS build pipeline from source to bootable images.
  20. *
  21. * Architecture:
  22. * 1. Clean previous output (preserving build caches)
  23. * 2. Stage frameworks, boot assets, vendor blobs, and service configs
  24. * 3. Compile all system binaries (init, arkrt, splash, display daemon, UI
  25. * app)
  26. * 4. Build the SwiftUI framework via SPM (cached — only rebuilds on changes)
  27. * 5. Sign the runtime binary for Verified Boot
  28. * 6. Pack initramfs with all runtime binaries
  29. * 7. Assemble bootloader and generate disk images
  30. *
  31. * All intermediate artifacts go into out_staging/. Final outputs go into
  32. * finished/. The .swift_build/ directory inside out_staging/ is preserved
  33. * across builds to enable SPM incremental compilation caching.
  34. *
  35. * Usage: build <arkos-base-dir>
  36. */
  37. #include "ark_parser.h"
  38. #include <dirent.h>
  39. #include <stdio.h>
  40. #include <stdlib.h>
  41. #include <string.h>
  42. #include <sys/stat.h>
  43. #include <unistd.h>
  44. /* ── Build Configuration ───────────────────────────────────────── */
  45. #define TOTAL_STEPS 16
  46. #define RPI4_TOTAL_STEPS 10
  47. static int current_step = 0;
  48. static int total_steps = TOTAL_STEPS;
  49. /* ── Progress Reporting ────────────────────────────────────────── */
  50. static void step(const char *desc) {
  51. current_step++;
  52. printf("\n\033[1;36m[%2d/%d]\033[0m %s\n", current_step, total_steps, desc);
  53. }
  54. /* ── Command Execution ─────────────────────────────────────────── */
  55. /**
  56. * Execute a shell command and print it. Non-zero exit codes are reported
  57. * but do NOT abort the build — callers must check return values for
  58. * critical steps.
  59. */
  60. static int run(const char *cmd) {
  61. printf(" \033[0;90m$ %s\033[0m\n", cmd);
  62. int ret = system(cmd);
  63. if (ret != 0) {
  64. fprintf(stderr, " \033[1;31m✗ Command failed (exit %d)\033[0m\n", ret);
  65. }
  66. return ret;
  67. }
  68. /* ── Path Utilities ────────────────────────────────────────────── */
  69. /** Join two path components. Caller must free the returned string. */
  70. static char *pjoin(const char *a, const char *b) {
  71. size_t len = strlen(a) + 1 + strlen(b) + 1;
  72. char *out = malloc(len);
  73. snprintf(out, len, "%s/%s", a, b);
  74. return out;
  75. }
  76. /* ── Incremental Build Helpers ─────────────────────────────────── */
  77. /**
  78. * Check if an output file exists and is newer than all listed source files.
  79. * Returns 1 if the output is up-to-date (build can be skipped), 0 otherwise.
  80. *
  81. * This enables incremental builds — Swift compilation is expensive, so we
  82. * only recompile when source files have actually changed.
  83. */
  84. static int is_up_to_date(const char *output, const char **sources,
  85. int source_count) {
  86. struct stat out_st;
  87. if (stat(output, &out_st) != 0) {
  88. return 0; /* Output doesn't exist — must build */
  89. }
  90. for (int i = 0; i < source_count; i++) {
  91. struct stat src_st;
  92. if (stat(sources[i], &src_st) != 0) {
  93. return 0; /* Source missing — must build */
  94. }
  95. if (src_st.st_mtime > out_st.st_mtime) {
  96. return 0; /* Source is newer — must rebuild */
  97. }
  98. }
  99. return 1; /* All sources are older than output — skip */
  100. }
  101. /* ── RPi4 Build Pipeline ───────────────────────────────────────── */
  102. /* ── Main Build Pipeline ───────────────────────────────────────── */
  103. int main(int argc, char *argv[]) {
  104. if (argc < 2) {
  105. fprintf(stderr, "Usage: %s <arkos-base-dir> [--device rpi4] [--no-image]\n",
  106. argv[0]);
  107. return 1;
  108. }
  109. const char *base = argv[1]; /* e.g. /home/user/repo/arkos */
  110. int is_rpi4 = 0;
  111. int no_image =
  112. 0; /* When set, skip merging individual .img files into one rpi4.img */
  113. for (int i = 2; i < argc; i++) {
  114. if (strcmp(argv[i], "--device") == 0 && i + 1 < argc) {
  115. if (strcmp(argv[i + 1], "rpi4") == 0) {
  116. is_rpi4 = 1;
  117. } else {
  118. fprintf(stderr, "Unknown device: %s\n", argv[i + 1]);
  119. return 1;
  120. }
  121. } else if (strcmp(argv[i], "--no-image") == 0) {
  122. no_image = 1;
  123. }
  124. }
  125. /* Derive the repo root (one level up from the arkos directory) */
  126. char *repo = pjoin(base, "..");
  127. /* Output and staging directories */
  128. char *out_dir =
  129. is_rpi4 ? pjoin(base, "finished/rpi4") : pjoin(base, "finished");
  130. char *staging =
  131. is_rpi4 ? pjoin(base, "out_staging/rpi4") : pjoin(base, "out_staging");
  132. char *staging_sys = pjoin(staging, "system");
  133. char *staging_fw = pjoin(staging, "system/frameworks");
  134. char *staging_vend = pjoin(staging, "vendor");
  135. char *staging_boot = pjoin(staging, "boot");
  136. /* Source directories */
  137. char *fw_dir = pjoin(base, "frameworks");
  138. char *boot_dir = pjoin(base, "boot");
  139. char *vendor_dir = pjoin(base, "vendor");
  140. char *system_dir = pjoin(base, "system");
  141. char *kernel_dir = pjoin(base, "kernel");
  142. printf("\n\033[1;35m╔══════════════════════════════════════╗\033[0m\n");
  143. printf("\033[1;35m║ ArkOS Build System v3.0 ║\033[0m\n");
  144. if (is_rpi4) {
  145. printf("\033[1;35m║ Target: ARM64 (Raspberry Pi) ║\033[0m\n");
  146. } else {
  147. printf("\033[1;35m║ Target: x86_64 (PC / QEMU) ║\033[0m\n");
  148. }
  149. printf("\033[1;35m╚══════════════════════════════════════╝\033[0m\n");
  150. /* ─── Step 1: Clean previous output ────────────────────────── */
  151. step("Cleaning previous output (preserving build caches)");
  152. {
  153. char cmd[1024];
  154. /* Preserve out_staging/ entirely for incremental builds.
  155. * Only wipe finished/ which contains the final disk images. */
  156. snprintf(cmd, sizeof(cmd), "rm -rf %s", out_dir);
  157. run(cmd);
  158. }
  159. /* ─── Step 2: Create staging directories ───────────────────── */
  160. step("Creating staging directories");
  161. {
  162. char cmd[1024];
  163. snprintf(cmd, sizeof(cmd), "mkdir -p %s %s %s %s %s/services", out_dir,
  164. staging_fw, staging_vend, staging_boot, staging_sys);
  165. run(cmd);
  166. }
  167. /* ─── Step 3: Stage frameworks ─────────────────────────────── */
  168. step("Staging frameworks");
  169. {
  170. char rm_cmd[1024];
  171. snprintf(rm_cmd, sizeof(rm_cmd), "rm -rf %s", staging_fw);
  172. run(rm_cmd);
  173. char mk_cmd[1024];
  174. snprintf(mk_cmd, sizeof(mk_cmd), "mkdir -p %s", staging_fw);
  175. run(mk_cmd);
  176. DIR *d = opendir(fw_dir);
  177. if (d) {
  178. struct dirent *ent;
  179. while ((ent = readdir(d)) != NULL) {
  180. if (ent->d_name[0] == '.')
  181. continue;
  182. /* DRM framework is excluded from the base system image */
  183. if (strcmp(ent->d_name, "DRM") == 0) {
  184. printf(" Skipping excluded framework: %s\n", ent->d_name);
  185. continue;
  186. }
  187. char cmd[1024];
  188. snprintf(cmd, sizeof(cmd), "cp -ur %s/%s %s/", fw_dir, ent->d_name,
  189. staging_fw);
  190. run(cmd);
  191. }
  192. closedir(d);
  193. }
  194. }
  195. /* ─── Step 4: Stage boot assets ────────────────────────────── */
  196. step("Staging boot assets");
  197. {
  198. DIR *d = opendir(boot_dir);
  199. if (d) {
  200. struct dirent *ent;
  201. while ((ent = readdir(d)) != NULL) {
  202. if (ent->d_name[0] == '.')
  203. continue;
  204. char cmd[1024];
  205. snprintf(cmd, sizeof(cmd), "cp -ur %s/%s %s/", boot_dir, ent->d_name,
  206. staging_boot);
  207. run(cmd);
  208. }
  209. closedir(d);
  210. }
  211. }
  212. /* ─── Step 5: Stage system services ────────────────────────── */
  213. step("Staging system service configs");
  214. {
  215. char cmd[1024];
  216. snprintf(cmd, sizeof(cmd),
  217. "cp -ur %s/system/services/* %s/services/ 2>/dev/null || true",
  218. base, staging_sys);
  219. run(cmd);
  220. }
  221. /* ─── Step 6: Stage vendor files ───────────────────────────── */
  222. step("Staging vendor files");
  223. {
  224. char cmd[1024];
  225. snprintf(cmd, sizeof(cmd), "cp -ur %s/* %s/ 2>/dev/null || true",
  226. vendor_dir, staging_vend);
  227. run(cmd);
  228. /* Exclude private Verified Boot key from public vendor partition */
  229. snprintf(cmd, sizeof(cmd),
  230. "rm -f %s/verify/securebuild.ark %s/securebuild.ark 2>/dev/null "
  231. "|| true",
  232. staging_vend, staging_vend);
  233. run(cmd);
  234. }
  235. /* ─── Step 7: Verify kernel configuration ──────────────────── */
  236. step("Verifying kernel configuration");
  237. {
  238. char *kconfig = pjoin(kernel_dir, ".config");
  239. if (access(kconfig, F_OK) != -1) {
  240. printf(" \033[0;32m✓\033[0m Kernel .config present\n");
  241. } else {
  242. printf(" \033[1;33m⚠ WARNING:\033[0m Kernel .config not found!\n");
  243. }
  244. free(kconfig);
  245. }
  246. /* ─── Step 8: Compile fb_helper.c ──────────────────────────── */
  247. step(is_rpi4 ? "Cross-compiling fb_helper.c (ARM64)"
  248. : "Compiling fb_helper.c (framebuffer ioctl bridge)");
  249. {
  250. char cmd[1024];
  251. snprintf(cmd, sizeof(cmd), "make -C %s %s/fb_helper.o", base,
  252. is_rpi4 ? "out_staging/rpi4" : "out_staging");
  253. run(cmd);
  254. }
  255. /* ─── Step 9: Compile Swift system binaries (incremental) ──── */
  256. step(is_rpi4
  257. ? "Cross-compiling Swift system binaries (ARM64)"
  258. : "Compiling Swift system binaries (splash, arkrt, display daemon)");
  259. {
  260. /* Check each binary individually for incremental builds */
  261. char arkrt_out[512], daemon_out[512];
  262. snprintf(arkrt_out, sizeof(arkrt_out), "%s/arkrt", staging);
  263. snprintf(daemon_out, sizeof(daemon_out), "%s/system/ui_daemon", staging);
  264. /* arkrt sources */
  265. char a1[512], a2[512], a3[512], a4[512], a5[512], a6[512], a7[512];
  266. snprintf(a1, sizeof(a1), "%s/arkrt/main.swift", base);
  267. snprintf(a2, sizeof(a2), "%s/arkrt/KernelBridge.swift", base);
  268. snprintf(a3, sizeof(a3), "%s/arkrt/CommandRouter.swift", base);
  269. snprintf(a4, sizeof(a4), "%s/arkrt/IPC.swift", base);
  270. snprintf(a5, sizeof(a5), "%s/arkrt/ServiceManager.swift", base);
  271. snprintf(a6, sizeof(a6), "%s/arkrt/NetworkService.swift", base);
  272. snprintf(a7, sizeof(a7), "%s/arkrt/InputService.swift", base);
  273. const char *arkrt_sources[] = {a1, a2, a3, a4, a5, a6, a7};
  274. if (is_up_to_date(arkrt_out, arkrt_sources, 7)) {
  275. printf(" \033[0;32m✓\033[0m arkrt is up-to-date (skipping)\n");
  276. } else {
  277. char cmd[1024];
  278. snprintf(cmd, sizeof(cmd), "make -C %s %s/arkrt", base,
  279. is_rpi4 ? "out_staging/rpi4" : "out_staging");
  280. run(cmd);
  281. }
  282. /* ark_compositor sources */
  283. char c1[512], c2[512], c3[512], c4[512];
  284. snprintf(c1, sizeof(c1), "%s/arkrt/compositor/main.swift", base);
  285. snprintf(c2, sizeof(c2), "%s/arkrt/compositor/Compositor.swift", base);
  286. snprintf(c3, sizeof(c3), "%s/arkrt/compositor/ClientProtocol.swift", base);
  287. snprintf(c4, sizeof(c4), "%s/arkrt/compositor/drm_helper.c", base);
  288. const char *comp_sources[] = {c1, c2, c3, c4};
  289. char comp_out[512];
  290. snprintf(comp_out, sizeof(comp_out), "%s/system/ark_compositor", staging);
  291. if (is_up_to_date(comp_out, comp_sources, 4)) {
  292. printf(" \033[0;32m✓\033[0m ark_compositor is up-to-date (skipping)\n");
  293. } else {
  294. char cmd[1024];
  295. }
  296. }
  297. /* ─── Step 10: Verify ArkGraphics framework ────────────────── */
  298. step(is_rpi4 ? "Verifying graphics & input components (ARM64)"
  299. : "Verifying graphics & input components");
  300. {
  301. printf(" \033[0;32m✓\033[0m ArkGraphics & ArkInput ready at "
  302. "arkrt/ArkGraphics\n");
  303. }
  304. /* ─── Step 11: Compile setup_app (incremental) ─────────────── */
  305. step(is_rpi4 ? "Cross-compiling setup_app (ARM64)"
  306. : "Compiling setup_app (setup app)");
  307. {
  308. char ut_out[512];
  309. snprintf(ut_out, sizeof(ut_out), "%s/system/setup_app", staging);
  310. char u1[512], u2[512], u3[512], u4[512], u5[512], u6[512], u7[512], u8[512];
  311. snprintf(u1, sizeof(u1), "%s/system/apps/setup_app.swift", base);
  312. snprintf(u2, sizeof(u2), "%s/arkrt/ArkGraphics/ArkGraphics.swift", base);
  313. snprintf(u3, sizeof(u3), "%s/arkrt/ArkGraphics/ArkWrite.swift", base);
  314. snprintf(u4, sizeof(u4), "%s/arkrt/ArkGraphics/ArkShapes.swift", base);
  315. snprintf(u5, sizeof(u5), "%s/arkrt/ArkGraphics/ArkFontRobotoBold.swift",
  316. base);
  317. snprintf(u6, sizeof(u6), "%s/arkrt/ArkGraphics/ArkInput.swift", base);
  318. snprintf(u7, sizeof(u7), "%s/arkrt/ArkGraphics/ArkUI.swift", base);
  319. snprintf(u8, sizeof(u8), "%s/arkrt/ArkGraphics/ArkIcons.swift", base);
  320. const char *ut_sources[] = {u1, u2, u3, u4, u5, u6, u7, u8};
  321. if (is_up_to_date(ut_out, ut_sources, 8)) {
  322. printf(" \033[0;32m✓\033[0m setup_app is up-to-date (skipping)\n");
  323. } else {
  324. char cmd[1024];
  325. snprintf(cmd, sizeof(cmd), "make -C %s %s/system/setup_app", base,
  326. is_rpi4 ? "out_staging/rpi4" : "out_staging");
  327. run(cmd);
  328. }
  329. char ui_cmd[1024];
  330. snprintf(ui_cmd, sizeof(ui_cmd), "make -C %s %s/system/ui_daemon", base,
  331. is_rpi4 ? "out_staging/rpi4" : "out_staging");
  332. run(ui_cmd);
  333. }
  334. /* ─── Step 12: Sign arkrt (Verified Boot) ──────────────────── */
  335. step(is_rpi4 ? "Signing arkrt binary (Verified Boot, ARM64)"
  336. : "Signing arkrt binary (Verified Boot)");
  337. {
  338. char cmd[1024];
  339. snprintf(cmd, sizeof(cmd),
  340. "python3 %s/verify/sign.py %s/verify/securebuild.ark "
  341. "%s/arkrt %s/signature.bin",
  342. vendor_dir, vendor_dir, staging, staging);
  343. run(cmd);
  344. }
  345. /* ─── Step 13: (Skipped) init (PID 1) is now handled natively by arkrt ─── */
  346. /* ─── Step 14: Pack initramfs ──────────────────────────────── */
  347. step("Packing initramfs (base + runtime + display + UI)");
  348. {
  349. char *initramfs_ext = pjoin(staging, "initramfs_ext");
  350. char *base_initramfs = pjoin(boot_dir, "initramfs.img");
  351. char cmd[2048];
  352. /* Extract base initramfs (contains busybox, kernel modules, etc.) if
  353. * present */
  354. snprintf(cmd, sizeof(cmd), "rm -rf %s && mkdir -p %s", initramfs_ext,
  355. initramfs_ext);
  356. run(cmd);
  357. if (access(base_initramfs, F_OK) != -1) {
  358. snprintf(cmd, sizeof(cmd),
  359. "cd %s && zcat %s 2>/dev/null | cpio -id --no-preserve-owner "
  360. "2>/dev/null || true",
  361. initramfs_ext, base_initramfs);
  362. run(cmd);
  363. }
  364. /* Create required directories in initramfs */
  365. snprintf(cmd, sizeof(cmd),
  366. "mkdir -p %s/system/services %s/run %s/tmp %s/var/log",
  367. initramfs_ext, initramfs_ext, initramfs_ext, initramfs_ext);
  368. run(cmd);
  369. /* Copy arkrt directly as /init (PID 1) */
  370. snprintf(cmd, sizeof(cmd), "cp %s/arkrt %s/init && chmod +x %s/init",
  371. staging, initramfs_ext, initramfs_ext);
  372. run(cmd);
  373. snprintf(cmd, sizeof(cmd), "cp %s/signature.bin %s/signature.bin", staging,
  374. initramfs_ext);
  375. run(cmd);
  376. /* Copy display daemon and setup app into /system/ */
  377. snprintf(cmd, sizeof(cmd),
  378. "cp %s/system/ui_daemon %s/system/ui_daemon && "
  379. "chmod +x %s/system/ui_daemon 2>/dev/null || true",
  380. staging, initramfs_ext, initramfs_ext);
  381. run(cmd);
  382. snprintf(cmd, sizeof(cmd),
  383. "cp %s/system/setup_app %s/system/setup_app && "
  384. "chmod +x %s/system/setup_app 2>/dev/null || true",
  385. staging, initramfs_ext, initramfs_ext);
  386. run(cmd);
  387. /* Copy service definition files */
  388. snprintf(cmd, sizeof(cmd),
  389. "cp -r %s/services/* %s/system/services/ 2>/dev/null || true",
  390. staging_sys, initramfs_ext);
  391. run(cmd);
  392. /* Repack initramfs as gzip-compressed cpio archive */
  393. snprintf(cmd, sizeof(cmd),
  394. "cd %s && find . | cpio -H newc -o 2>/dev/null | gzip > "
  395. "%s/initramfs.img",
  396. initramfs_ext, out_dir);
  397. run(cmd);
  398. free(initramfs_ext);
  399. free(base_initramfs);
  400. }
  401. /* ─── Step 15: Assemble bootloader + stage2 ────────────────── */
  402. step("Assembling bootloader");
  403. {
  404. char cmd[1024];
  405. /* Generate boot animation frames from Python script */
  406. snprintf(cmd, sizeof(cmd),
  407. "python3 %s/source/animationframes/generate_frames.py %s",
  408. boot_dir, staging);
  409. run(cmd);
  410. snprintf(cmd, sizeof(cmd),
  411. "make -C %s out_staging/bootloader.bin out_staging/stage2.bin "
  412. "out_staging/BOOTX64.EFI",
  413. base);
  414. run(cmd);
  415. }
  416. /* ─── Step 16: Generate disk images ────────────────────────── */
  417. step(is_rpi4 ? "Generating disk images (RPi4 SD Card)"
  418. : "Generating disk images");
  419. {
  420. char cmd[2048];
  421. /* boot.img — bootloader + kernel + initramfs packed by pack_boot.py */
  422. if (!is_rpi4) {
  423. snprintf(cmd, sizeof(cmd), "python3 %s/tools/pack_boot.py --base-dir %s",
  424. repo, base);
  425. run(cmd);
  426. } else {
  427. /* Copy ARM64 kernel to finished/rpi4 as kernel8.img */
  428. snprintf(cmd, sizeof(cmd), "cp %s/prebuilts/arm64 %s/kernel8.img",
  429. kernel_dir, out_dir);
  430. run(cmd);
  431. snprintf(cmd, sizeof(cmd),
  432. "cp %s/prebuilts/*.dtb %s/ 2>/dev/null || true", kernel_dir,
  433. out_dir);
  434. run(cmd);
  435. snprintf(cmd, sizeof(cmd),
  436. "mkdir -p %s/overlays && cp %s/prebuilts/rpi4_dtbofiles/*.dtbo "
  437. "%s/overlays/ 2>/dev/null || true",
  438. out_dir, kernel_dir, out_dir);
  439. run(cmd);
  440. }
  441. /* sys.img — system partition (frameworks, apps, services) */
  442. snprintf(cmd, sizeof(cmd),
  443. "dd if=/dev/zero of=%s/sys.img bs=1M count=2048 2>/dev/null",
  444. out_dir);
  445. run(cmd);
  446. snprintf(cmd, sizeof(cmd), "mkfs.ext4 -d %s %s/sys.img 2>/dev/null",
  447. staging_sys, out_dir);
  448. run(cmd);
  449. /* vend.img — vendor partition (OEM blobs, keys) */
  450. snprintf(cmd, sizeof(cmd),
  451. "dd if=/dev/zero of=%s/vend.img bs=1M count=100 2>/dev/null",
  452. out_dir);
  453. run(cmd);
  454. snprintf(cmd, sizeof(cmd),
  455. "mkfs.ext4 -F -d %s %s/vend.img 2>/dev/null || mkfs.ext4 -F "
  456. "%s/vend.img 2>/dev/null",
  457. staging_vend, out_dir, out_dir);
  458. run(cmd);
  459. /* ── dtbo.img — Device Tree Blobs + Overlays ────────────── */
  460. /* Stage all .dtb files from kernel/prebuilts/ and all .dtbo files
  461. * from kernel/prebuilts/rpi4_dtbofiles/ into a temp directory,
  462. * then pack them into an ext4 image. The bootloader extracts
  463. * these and feeds them to the kernel. */
  464. {
  465. char *dtbo_stage = pjoin(staging, "dtbo_staging");
  466. snprintf(cmd, sizeof(cmd), "rm -rf %s && mkdir -p %s/overlays",
  467. dtbo_stage, dtbo_stage);
  468. run(cmd);
  469. /* Copy .dtb files (base device trees) */
  470. snprintf(cmd, sizeof(cmd),
  471. "cp %s/prebuilts/*.dtb %s/ 2>/dev/null || true", kernel_dir,
  472. dtbo_stage);
  473. run(cmd);
  474. /* Copy .dtbo files (overlays) */
  475. snprintf(cmd, sizeof(cmd),
  476. "cp %s/prebuilts/rpi4_dtbofiles/*.dtbo %s/overlays/ 2>/dev/null "
  477. "|| true",
  478. kernel_dir, dtbo_stage);
  479. run(cmd);
  480. /* Also copy any .dtb files that are in the rpi4_dtbofiles dir */
  481. snprintf(cmd, sizeof(cmd),
  482. "cp %s/prebuilts/rpi4_dtbofiles/*.dtb %s/overlays/ 2>/dev/null "
  483. "|| true",
  484. kernel_dir, dtbo_stage);
  485. run(cmd);
  486. /* Calculate size: round up to nearest MB + 2MB headroom */
  487. snprintf(cmd, sizeof(cmd),
  488. "du -sm %s | awk '{print ($1 < 4 ? 4 : $1 + 2)}'", dtbo_stage);
  489. FILE *p = popen(cmd, "r");
  490. int dtbo_size_mb = 4; /* default fallback */
  491. if (p) {
  492. fscanf(p, "%d", &dtbo_size_mb);
  493. pclose(p);
  494. }
  495. printf(" dtbo.img size: %d MB\n", dtbo_size_mb);
  496. snprintf(cmd, sizeof(cmd),
  497. "dd if=/dev/zero of=%s/dtbo.img bs=1M count=%d 2>/dev/null",
  498. out_dir, dtbo_size_mb);
  499. run(cmd);
  500. snprintf(cmd, sizeof(cmd), "mkfs.ext4 -d %s %s/dtbo.img 2>/dev/null",
  501. dtbo_stage, out_dir);
  502. run(cmd);
  503. printf(" \033[0;32m✓\033[0m dtbo.img packed with device tree blobs and "
  504. "overlays\n");
  505. free(dtbo_stage);
  506. }
  507. /* ── vbk.img — Verified Boot Key ───────────────────────────── */
  508. /* Contains the securebuild.ark signing key. The bootloader reads
  509. * the VBK key from here and verifies that sys.img and vend.img
  510. * were signed with the matching key. */
  511. {
  512. char *securebuild_path = pjoin(vendor_dir, "verify/securebuild.ark");
  513. ArkConfig *sbc = ark_parse(securebuild_path);
  514. int has_keys = 0;
  515. for (int i = 0; i < sbc->standalone_count; i++) {
  516. if (strstr(sbc->standalone[i], "ARK-OS-") != NULL) {
  517. has_keys = 1;
  518. break;
  519. }
  520. }
  521. if (has_keys) {
  522. printf(" \033[0;32m✓\033[0m VBK signing keys found — generating "
  523. "vbk.img\n");
  524. char *vbk_stage = pjoin(staging, "vbk_staging");
  525. snprintf(cmd, sizeof(cmd), "rm -rf %s && mkdir -p %s", vbk_stage,
  526. vbk_stage);
  527. run(cmd);
  528. snprintf(cmd, sizeof(cmd), "cp %s %s/securebuild.ark", securebuild_path,
  529. vbk_stage);
  530. run(cmd);
  531. snprintf(cmd, sizeof(cmd),
  532. "dd if=/dev/zero of=%s/vbk.img bs=1M count=1 2>/dev/null",
  533. out_dir);
  534. run(cmd);
  535. snprintf(cmd, sizeof(cmd), "mkfs.ext4 -d %s %s/vbk.img 2>/dev/null",
  536. vbk_stage, out_dir);
  537. run(cmd);
  538. printf(" \033[0;32m✓\033[0m vbk.img packed with Verified Boot Key\n");
  539. free(vbk_stage);
  540. } else {
  541. printf(" \033[1;33m⚠\033[0m No signing keys — skipping vbk.img\n");
  542. }
  543. ark_free(sbc);
  544. free(securebuild_path);
  545. }
  546. /* ── vbmeta.img — Boot Metadata / Mount Configuration ──── */
  547. /* Contains vbmeta.ark which tells the bootloader the partition
  548. * layout and how to mount each image. Loaded first during boot. */
  549. {
  550. char *vbmeta_stage = pjoin(staging, "vbmeta_staging");
  551. char *vbmeta_ark = pjoin(boot_dir, "vbmeta.ark");
  552. snprintf(cmd, sizeof(cmd), "rm -rf %s && mkdir -p %s", vbmeta_stage,
  553. vbmeta_stage);
  554. run(cmd);
  555. if (access(vbmeta_ark, F_OK) != -1) {
  556. snprintf(cmd, sizeof(cmd), "cp %s %s/vbmeta.ark", vbmeta_ark,
  557. vbmeta_stage);
  558. run(cmd);
  559. } else {
  560. printf(" \033[1;33m⚠\033[0m boot/vbmeta.ark not found — vbmeta.img "
  561. "will be empty\n");
  562. }
  563. snprintf(cmd, sizeof(cmd),
  564. "dd if=/dev/zero of=%s/vbmeta.img bs=1M count=1 2>/dev/null",
  565. out_dir);
  566. run(cmd);
  567. snprintf(cmd, sizeof(cmd), "mkfs.ext4 -d %s %s/vbmeta.img 2>/dev/null",
  568. vbmeta_stage, out_dir);
  569. run(cmd);
  570. printf(
  571. " \033[0;32m✓\033[0m vbmeta.img packed with mount configuration\n");
  572. free(vbmeta_stage);
  573. free(vbmeta_ark);
  574. }
  575. /* ── rpi4.img — Assemble all images into one flashable SD card image ── */
  576. if (is_rpi4 && !no_image) {
  577. printf("\n Assembling rpi4.img (flashable SD card image)...\n");
  578. snprintf(cmd, sizeof(cmd), "python3 %s/tools/pack_rpi4.py --base-dir %s",
  579. repo, base);
  580. run(cmd);
  581. } else if (is_rpi4 && no_image) {
  582. printf("\n \033[1;33m[--no-image]\033[0m Skipping rpi4.img assembly — "
  583. "individual .img files available in %s/\n",
  584. out_dir);
  585. }
  586. }
  587. printf("\n\033[1;32m╔══════════════════════════════════════╗\033[0m\n");
  588. printf("\033[1;32m║ Build Complete! ║\033[0m\n");
  589. printf("\033[1;32m╚══════════════════════════════════════╝\033[0m\n");
  590. printf("Run \033[1mmake test-uefi or make test-bios (if x86_64) \033[0m or "
  591. "\033[1mmake test-uefi-arm64 or make test-bios-arm64 (if "
  592. "aarch64)\033[0m to launch ArkOS in QEMU.\n\n");
  593. /* Free all heap-allocated path strings */
  594. free(repo);
  595. free(out_dir);
  596. free(staging);
  597. free(staging_sys);
  598. free(staging_fw);
  599. free(staging_vend);
  600. free(staging_boot);
  601. free(fw_dir);
  602. free(boot_dir);
  603. free(vendor_dir);
  604. free(system_dir);
  605. free(kernel_dir);
  606. return 0;
  607. }