Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 17 additions & 1 deletion examples/elf/main/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -69,8 +69,24 @@ ifeq ($(CONFIG_EXAMPLES_ELF_ROMFS),y)
ROMFSIMG = elf_romfs.img
ROMFSSRC = elf_romfs.c
ROMFSOBJ = $(ROMFSSRC:.c=$(OBJEXT))
ifdef CONFIG_SYSTEM_NXPKG
PKGINDEX = $(BINDIR)/index.json
PKGBADINDEX = $(BINDIR)/bad-index.json
PKGTEST = $(BINDIR)/pkgtest.nsh
PKGFAIL = $(BINDIR)/pkgfail.nsh
PKG_FIXTURE_GEN = $(APPDIR)/examples/elf/main/mk_pkg_fixture.py
PKG_FIXTURE_OUTPUTS = $(PKGINDEX) $(PKGBADINDEX) $(PKGTEST) $(PKGFAIL)
PKG_FIXTURE_INPUTS = $(BINDIR)/hello
PKG_FIXTURE_ARGS = \
$(BINDIR)/hello $(PKGINDEX) $(PKGBADINDEX) $(PKGTEST) $(PKGFAIL) \
$(CONFIG_ARCH) $(CONFIG_ARCH_BOARD)

$(PKG_FIXTURE_OUTPUTS) &: $(PKG_FIXTURE_INPUTS) $(PKG_FIXTURE_GEN)
$(Q) python3 $(PKG_FIXTURE_GEN) \
$(PKG_FIXTURE_ARGS)
endif

$(ROMFSIMG):
$(ROMFSIMG): $(PKG_FIXTURE_OUTPUTS)
$(Q) genromfs -d $(BINDIR) -f $@

$(ROMFSSRC): $(ROMFSIMG)
Expand Down
127 changes: 127 additions & 0 deletions examples/elf/main/mk_pkg_fixture.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,127 @@
#!/usr/bin/env python3
#
# SPDX-License-Identifier: Apache-2.0

import hashlib
import json
import pathlib
import sys


def sha256_file(path: pathlib.Path) -> str:
digest = hashlib.sha256()
with path.open("rb") as infile:
while True:
chunk = infile.read(4096)
if not chunk:
break
digest.update(chunk)

return digest.hexdigest()


def write_index(path: pathlib.Path, arch: str, compat: str,
artifact: str, digest: str) -> None:
payload = {
"packages": [
{
"name": "hello",
"version": "1.0.0",
"arch": arch,
"compat": compat,
"artifact": artifact,
"sha256": digest,
"type": "elf",
},
{
"name": "hello",
"version": "9.9.9",
"arch": "arm",
"compat": "stm32f4discovery",
"artifact": artifact,
"sha256": digest,
"type": "elf",
}
]
}

path.write_text(json.dumps(payload, separators=(",", ":")) + "\n",
encoding="utf-8")


def write_bad_index(path: pathlib.Path, arch: str, compat: str) -> None:
payload = {
"packages": [
{
"name": "hello-missing",
"version": "1.0.0",
"arch": arch,
"compat": compat,
"artifact": "/mnt/elf/romfs/hello-missing",
"sha256": "0" * 64,
"type": "elf",
}
]
}

path.write_text(json.dumps(payload, separators=(",", ":")) + "\n",
encoding="utf-8")


def write_script(path: pathlib.Path) -> None:
script = "\n".join(
[
"mount -t tmpfs /etc",
"mount -t tmpfs /var",
"mkdir /etc/nxpkg",
"cp /mnt/elf/romfs/index.json /etc/nxpkg/index.json",
"nxpkg install hello",
"nxpkg list",
"",
]
)
path.write_text(script, encoding="utf-8")


def write_fail_script(path: pathlib.Path) -> None:
script = "\n".join(
[
"mount -t tmpfs /etc",
"mount -t tmpfs /var",
"mkdir /etc/nxpkg",
"cp /mnt/elf/romfs/bad-index.json /etc/nxpkg/index.json",
"nxpkg install hello-missing",
"",
]
)
path.write_text(script, encoding="utf-8")


def main() -> int:
if len(sys.argv) != 8:
print(
"usage: mk_pkg_fixture.py <hello-bin> <index-json> <bad-index-json> "
"<pkgtest-nsh> <pkgfail-nsh> <arch> <compat>",
file=sys.stderr,
)
return 1

hello = pathlib.Path(sys.argv[1])
index = pathlib.Path(sys.argv[2])
bad_index = pathlib.Path(sys.argv[3])
script = pathlib.Path(sys.argv[4])
fail_script = pathlib.Path(sys.argv[5])
arch = sys.argv[6]
compat = sys.argv[7]
artifact = "/mnt/elf/romfs/hello"

digest = sha256_file(hello)
write_index(index, arch, compat, artifact, digest)
write_bad_index(bad_index, arch, compat)
write_script(script)
write_fail_script(fail_script)
return 0


if __name__ == "__main__":
raise SystemExit(main())
Loading