diff --git a/ARCHITECTURE.md b/ARCHITECTURE.md index 82cdbd4..5ea92fb 100644 --- a/ARCHITECTURE.md +++ b/ARCHITECTURE.md @@ -53,4 +53,4 @@ Target `$ROOTFS` installs only pure **Alpine Edge** (`main`, `community`, `testi - **BROM/DA Flasher**: [flash-mtk.py](flash-mtk.py) uses vendored `mtkclient` + `MTK_DA_V5_MT6797.bin`, write-blocking protected factory partitions (`LBA 0..0x6FFFF` / `0x0E000000`, verified in [scripts/test-flash-mtk-gpt.py](scripts/test-flash-mtk-gpt.py)). - **Staged ext4 Customization**: On temporary ext4 copies, [scripts/extract-vendor-fw.py](scripts/extract-vendor-fw.py) injects MT6797 firmware blobs via `debugfs`/`e2fsck`, [scripts/geminux_accounts.py](scripts/geminux_accounts.py) provisions SHA-512 `/etc/{passwd,group,shadow}` + `/home/`, and [scripts/geminux_luks.py](scripts/geminux_luks.py) optionally applies offline LUKS2 encryption (`cryptsetup reencrypt`). -- **Release ZIP Packager**: [package-image.sh](package-image.sh) ([scripts/package-release-zip.py](scripts/package-release-zip.py)) clones and stages `geminux-rootfs.img` (stripping SSH keys while keeping `/lib/firmware/mediatek/mt6797/` blobs by default; `--strip-firmware` strips blobs, `--keep-ssh-keys` preserves SSH keys), runs `e2fsck -fn`, generates `SHA256SUMS.txt` + `FLASH-INSTRUCTIONS.txt`, and writes a verified ZIP64 release archive + `.sha256`. +- **Release ZIP Packager**: [package-image.sh](package-image.sh) ([scripts/package-release-zip.py](scripts/package-release-zip.py)) versions the release archive from the latest git commit (`-`), clones and stages `geminux-rootfs.img` (stripping SSH keys while keeping `/lib/firmware/mediatek/mt6797/` blobs by default; `--strip-firmware` strips blobs, `--keep-ssh-keys` preserves SSH keys), runs `e2fsck -fn`, generates `SHA256SUMS.txt` + `FLASH-INSTRUCTIONS.txt`, and writes a verified ZIP64 release archive + `.sha256`. diff --git a/INSTALL.md b/INSTALL.md index bbb636c..c9be1f9 100644 --- a/INSTALL.md +++ b/INSTALL.md @@ -42,7 +42,7 @@ Create a self-contained ZIP archive under `releases/` containing the images, `SH ./package-image.sh # or ./rebuild-image.sh --package ``` -By default, [scripts/package-release-zip.py](scripts/package-release-zip.py) stages a copy of `geminux-rootfs.img`, strips SSH host/authorized keys while keeping `/lib/firmware/mediatek/mt6797/` blobs (`--include-firmware` default), verifies `e2fsck -fn` and ZIP CRCs, and writes `.zip.sha256` (`--strip-firmware` omits proprietary blobs for clean redistribution; `--keep-ssh-keys` keeps SSH keys). +By default, [scripts/package-release-zip.py](scripts/package-release-zip.py) versions the archive from the latest git commit (`-`, overridable with `--version` or `GEMINUX_VERSION`), stages a copy of `geminux-rootfs.img`, strips SSH host/authorized keys while keeping `/lib/firmware/mediatek/mt6797/` blobs (`--include-firmware` default), verifies `e2fsck -fn` and ZIP CRCs, and writes `.zip.sha256` (`--strip-firmware` omits proprietary blobs for clean redistribution; `--keep-ssh-keys` keeps SSH keys). ## Flashing to the Gemini PDA diff --git a/scripts/package-release-zip.py b/scripts/package-release-zip.py index 9eed363..1604627 100755 --- a/scripts/package-release-zip.py +++ b/scripts/package-release-zip.py @@ -49,6 +49,44 @@ def _query_version_field(flag, fallback): return val if res.returncode == 0 and val else fallback +def _query_git_commit_version(repo_dir=BASE_DIR): + """Derive - from the latest git commit in repo_dir.""" + try: + res = subprocess.run( + ["git", "-c", f"safe.directory={repo_dir}", "-C", repo_dir, "log", "-1", "--format=%ct %h"], + capture_output=True, text=True, check=False, + ) + except OSError: + return None + if res.returncode != 0: + return None + parts = res.stdout.strip().split() + if len(parts) != 2: + return None + try: + commit_ts = int(parts[0]) + except ValueError: + return None + short_sha = parts[1] + commit_date = datetime.datetime.fromtimestamp( + commit_ts, tz=datetime.timezone.utc + ).strftime("%Y%m%d") + return f"{commit_date}-{short_sha}" + + +def resolve_release_version(version=None, repo_dir=BASE_DIR): + """Resolve release version: explicit arg > GEMINUX_VERSION > git commit > version.sh fallback.""" + if version and version.strip(): + return version.strip() + env_ver = os.environ.get("GEMINUX_VERSION", "").strip() + if env_ver: + return env_ver + git_ver = _query_git_commit_version(repo_dir=repo_dir) + if git_ver: + return git_ver + return _query_version_field("--print", "unknown") + + def sha256_file(path): """Compute streaming SHA-256 hex digest for a file.""" h = hashlib.sha256() @@ -357,7 +395,7 @@ def package_release_zip( verify=True, ): """Create a redistributable Geminux release ZIP archive with TXT flashing guide.""" - ver = version or _query_version_field("--print", "0.1.0") + ver = resolve_release_version(version) cname = codename or _query_version_field("--print-codename", "helio") top_dir = f"geminux-{ver}-{cname}-gemini-x27" if not output_zip: @@ -470,7 +508,7 @@ def build_arg_parser(): p.add_argument("-o", "--output", help="Output ZIP file path (default: releases/geminux---gemini-x27.zip)") p.add_argument("--images-dir", default=os.path.join(BASE_DIR, "images"), help="Directory containing boot-geminux.img, recovery-geminux.img, geminux-rootfs.img") - p.add_argument("--version", help="Override Geminux version string") + p.add_argument("--version", help="Override Geminux release version string (default: recent git commit -, or GEMINUX_VERSION)") p.add_argument("--codename", help="Override Geminux codename") p.add_argument("--include-firmware", dest="include_firmware", action="store_true", default=True, help="Keep /lib/firmware/mediatek/mt6797 blobs inside packaged rootfs (default)") diff --git a/scripts/test-flash-mtk-gpt.py b/scripts/test-flash-mtk-gpt.py index 35cef4a..135e755 100755 --- a/scripts/test-flash-mtk-gpt.py +++ b/scripts/test-flash-mtk-gpt.py @@ -820,6 +820,7 @@ def test_firmware_injection_ext4(): def test_release_zip_packager(): + import datetime import zipfile if not all(shutil.which(cmd) for cmd in ("mke2fs", "debugfs", "e2fsck")): @@ -829,7 +830,38 @@ def test_release_zip_packager(): pkg_mod = _load_mod( "package_release_zip", os.path.join(SCRIPTS_DIR, "package-release-zip.py") ) + git_res = subprocess.run( + ["git", "-C", BASE_DIR, "log", "-1", "--format=%ct %h"], + capture_output=True, text=True, check=True, + ) + git_ts_str, git_sha = git_res.stdout.strip().split() + expected_date = datetime.datetime.fromtimestamp( + int(git_ts_str), tz=datetime.timezone.utc + ).strftime("%Y%m%d") + expected_ver = f"{expected_date}-{git_sha}" + with tempfile.TemporaryDirectory(prefix="geminux-zip-test-") as tmp: + saved_ver = os.environ.pop("GEMINUX_VERSION", None) + saved_cname = os.environ.pop("GEMINUX_CODENAME", None) + try: + assert pkg_mod.resolve_release_version() == expected_ver + assert pkg_mod.resolve_release_version() != "0.1.0" + assert pkg_mod.resolve_release_version("custom-1.2") == "custom-1.2" + os.environ["GEMINUX_VERSION"] = "env-9.9" + assert pkg_mod.resolve_release_version() == "env-9.9" + os.environ.pop("GEMINUX_VERSION", None) + fallback_ver = pkg_mod.resolve_release_version(repo_dir=tmp) + assert isinstance(fallback_ver, str) and len(fallback_ver) > 0 + finally: + if saved_ver is not None: + os.environ["GEMINUX_VERSION"] = saved_ver + else: + os.environ.pop("GEMINUX_VERSION", None) + if saved_cname is not None: + os.environ["GEMINUX_CODENAME"] = saved_cname + else: + os.environ.pop("GEMINUX_CODENAME", None) + sysroot = os.path.join(tmp, "sysroot") os.makedirs(os.path.join(sysroot, "etc", "ssh"), exist_ok=True) os.makedirs(os.path.join(sysroot, "root", ".ssh"), exist_ok=True) @@ -866,22 +898,24 @@ def test_release_zip_packager(): ) orig_root_sha = flash_mtk.sha256_file(root_img) - out_zip = os.path.join(tmp, "geminux-0.1.0-helio-gemini-x27.zip") + pkg_env = dict(os.environ) + pkg_env.pop("GEMINUX_VERSION", None) + pkg_env.pop("GEMINUX_CODENAME", None) + + top = f"geminux-{expected_ver}-helio-gemini-x27" + out_zip = os.path.join(tmp, f"{top}.zip") res = subprocess.run( [ os.path.join(BASE_DIR, "package-image.sh"), "--images-dir", img_dir, "--output", out_zip, - "--version", "0.1.0", - "--codename", "helio", ], - capture_output=True, text=True, check=False, + env=pkg_env, capture_output=True, text=True, check=False, ) assert res.returncode == 0, f"package-image.sh failed:\n{res.stdout}\n{res.stderr}" assert flash_mtk.sha256_file(root_img) == orig_root_sha, "Source rootfs image was mutated!" assert os.path.isfile(out_zip) and os.path.isfile(out_zip + ".sha256") - top = "geminux-0.1.0-helio-gemini-x27" with zipfile.ZipFile(out_zip, "r") as zf: assert zf.testzip() is None names = set(zf.namelist()) @@ -905,8 +939,16 @@ def test_release_zip_packager(): sh_info = zf.getinfo(f"{top}/flash-mtk.sh") assert ((sh_info.external_attr >> 16) & 0o111) == 0o111, "flash-mtk.sh missing +x attr" txt = zf.read(f"{top}/FLASH-INSTRUCTIONS.txt").decode("utf-8") - for needle in ("./flash-mtk.sh --flash-full", "sha256sum -c SHA256SUMS.txt", "LEFT USB-C port", "Docker is NOT required"): + for needle in ( + f"Release: {expected_ver} (helio)", + f"unzip {top}.zip", + "./flash-mtk.sh --flash-full", + "sha256sum -c SHA256SUMS.txt", + "LEFT USB-C port", + "Docker is NOT required", + ): assert needle in txt, f"FLASH-INSTRUCTIONS.txt missing {needle!r}" + assert "0.1.0" not in txt, "FLASH-INSTRUCTIONS.txt unexpectedly contains fallback version 0.1.0" # Extract with unzip to verify preserved executable bits, SHA256SUMS, and flasher launch ext_dir = os.path.join(tmp, "extracted") @@ -936,23 +978,24 @@ def test_release_zip_packager(): assert not fw_mod._debugfs_path_exists(sanitized_img, "/root/.ssh/authorized_keys") assert not fw_mod._debugfs_path_exists(sanitized_img, "/home/user/.ssh/authorized_keys") - stripped_zip = os.path.join(tmp, "geminux-0.1.0-helio-gemini-x27-stripped.zip") + stripped_top = "geminux-custom-ver-helio-gemini-x27" + stripped_zip = os.path.join(tmp, f"{stripped_top}-stripped.zip") sres = subprocess.run( [ os.path.join(BASE_DIR, "package-image.sh"), "--images-dir", img_dir, "--output", stripped_zip, - "--version", "0.1.0", + "--version", "custom-ver", "--codename", "helio", "--strip-firmware", ], - capture_output=True, text=True, check=False, + env=pkg_env, capture_output=True, text=True, check=False, ) assert sres.returncode == 0, f"package-image.sh --strip-firmware failed:\n{sres.stdout}\n{sres.stderr}" strip_dir = os.path.join(tmp, "extracted-stripped") os.makedirs(strip_dir, exist_ok=True) subprocess.run(["unzip", "-q", stripped_zip, "-d", strip_dir], check=True) - stripped_img = os.path.join(strip_dir, top, "images", "geminux-rootfs.img") + stripped_img = os.path.join(strip_dir, stripped_top, "images", "geminux-rootfs.img") assert subprocess.run(["e2fsck", "-fn", stripped_img], capture_output=True).returncode == 0 assert fw_mod.debugfs_ls(stripped_img, "lib/firmware/mediatek/mt6797") == [] print("[PASS] Release ZIP packager (sanitized rootfs, FLASH-INSTRUCTIONS.txt, SHA256SUMS, runnable flasher) verified")