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
9 changes: 8 additions & 1 deletion examples/sotest/Make.defs
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,11 @@
#
############################################################################

include $(wildcard $(APPDIR)/examples/sotest/*/Make.defs)
ifeq ($(CONFIG_EXAMPLES_SOTEST),y)
include $(APPDIR)/examples/sotest/main/Make.defs
endif

ifneq ($(CONFIG_EXAMPLES_SOTEST),n)
include $(APPDIR)/examples/sotest/modprint/Make.defs
include $(APPDIR)/examples/sotest/sotest/Make.defs
endif
14 changes: 13 additions & 1 deletion examples/sotest/main/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,20 @@ ifeq ($(CONFIG_EXAMPLES_SOTEST_BUILTINFS),y)
ROMFSIMG = sotest_romfs.img
ROMFSSRC = sotest_romfs.c
ROMFSOBJ = $(ROMFSSRC:.c=$(OBJEXT))
ifdef CONFIG_SYSTEM_NXPKG
PKGSHAREDINDEX = $(BINDIR)/shared-index.json
PKGSHAREDTEST = $(BINDIR)/pkgsotest.nsh
PKG_FIXTURE_GEN = $(APPDIR)/examples/sotest/main/mk_pkg_fixture_shared.py
PKG_FIXTURE_OUTPUTS = $(PKGSHAREDINDEX) $(PKGSHAREDTEST)

$(PKG_FIXTURE_OUTPUTS) &: $(BINDIR)/modprint $(BINDIR)/sotest $(PKG_FIXTURE_GEN)
$(Q) python3 $(PKG_FIXTURE_GEN) \
$(BINDIR)/modprint $(BINDIR)/sotest \
$(PKGSHAREDINDEX) $(PKGSHAREDTEST) \
$(CONFIG_ARCH) $(CONFIG_ARCH_BOARD)
endif

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

$(ROMFSSRC): $(ROMFSIMG)
Expand Down
112 changes: 112 additions & 0 deletions examples/sotest/main/mk_pkg_fixture_shared.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
#!/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_shared_index(path: pathlib.Path, arch: str, compat: str,
modprint_digest: str, sotest_digest: str) -> None:
payload = {
"packages": [
{
"name": "modprint",
"version": "1.0.0",
"arch": arch,
"compat": compat,
"artifact": "/mnt/sotest/romfs/modprint",
"sha256": modprint_digest,
"type": "shared-lib",
},
{
"name": "modprint",
"version": "9.9.9",
"arch": "arm",
"compat": "stm32f4discovery",
"artifact": "/mnt/sotest/romfs/modprint",
"sha256": modprint_digest,
"type": "shared-lib",
},
{
"name": "sotest",
"version": "1.0.0",
"arch": arch,
"compat": compat,
"artifact": "/mnt/sotest/romfs/sotest",
"sha256": sotest_digest,
"type": "shared-lib",
},
{
"name": "sotest",
"version": "9.9.9",
"arch": "arm",
"compat": "stm32f4discovery",
"artifact": "/mnt/sotest/romfs/sotest",
"sha256": sotest_digest,
"type": "shared-lib",
},
]
}

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


def write_shared_script(path: pathlib.Path) -> None:
script = "\n".join(
[
"mount -t tmpfs /etc",
"mount -t tmpfs /var",
"mkdir /etc/nxpkg",
"cp /mnt/sotest/romfs/shared-index.json /etc/nxpkg/index.json",
"nxpkg install modprint",
"nxpkg install sotest",
"cp /var/lib/nxpkg/pkgs/modprint/1.0.0/modprint /etc/m",
"cp /var/lib/nxpkg/pkgs/sotest/1.0.0/sotest /etc/s",
"sotest /etc/m /etc/s",
"nxpkg list",
"",
]
)
path.write_text(script, encoding="utf-8")


def main() -> int:
if len(sys.argv) != 7:
print(
"usage: mk_pkg_fixture_shared.py <modprint-bin> <sotest-bin> "
"<shared-index-json> <pkgsotest-nsh> <arch> <compat>",
file=sys.stderr,
)
return 1

modprint = pathlib.Path(sys.argv[1])
sotest = pathlib.Path(sys.argv[2])
shared_index = pathlib.Path(sys.argv[3])
shared_script = pathlib.Path(sys.argv[4])
arch = sys.argv[5]
compat = sys.argv[6]

write_shared_index(shared_index, arch, compat,
sha256_file(modprint), sha256_file(sotest))
write_shared_script(shared_script)
return 0


if __name__ == "__main__":
raise SystemExit(main())
98 changes: 87 additions & 11 deletions examples/sotest/main/sotest_main.c
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,21 @@

#define SOTEST_DEVPATH_FMT "/dev/ram%d"

/****************************************************************************
* Private Functions
****************************************************************************/

static void sotest_show_usage(FAR const char *progname)
{
#if CONFIG_LIBC_ELF_MAXDEPEND > 0
fprintf(stderr,
"Usage: %s [--mount] [<modprint-path> <sotest-path>]\n",
progname);
#else
fprintf(stderr, "Usage: %s [--mount] [<sotest-path>]\n", progname);
#endif
}

/****************************************************************************
* Symbols from Auto-Generated Code
****************************************************************************/
Expand All @@ -104,7 +119,11 @@ extern const int g_sot_nexports;

int main(int argc, FAR char *argv[])
{
FAR const char *modprint_path = NULL;
FAR const char *sotest_path = NULL;
bool mount_only = false;
#ifdef CONFIG_EXAMPLES_SOTEST_BUILTINFS
bool mounted_builtinfs = false;
char devname[32];
#endif
#if CONFIG_LIBC_ELF_MAXDEPEND > 0
Expand All @@ -117,6 +136,38 @@ int main(int argc, FAR char *argv[])
#ifdef CONFIG_EXAMPLES_SOTEST_BUILTINFS
struct boardioc_romdisk_s desc;
#endif

#if CONFIG_LIBC_ELF_MAXDEPEND > 0
if (argc == 2 && strcmp(argv[1], "--mount") == 0)
{
mount_only = true;
}
else if (argc == 3)
{
modprint_path = argv[1];
sotest_path = argv[2];
}
else if (argc != 1)
{
sotest_show_usage(argv[0]);
return EXIT_FAILURE;
}
#else
if (argc == 2 && strcmp(argv[1], "--mount") == 0)
{
mount_only = true;
}
else if (argc == 2)
{
sotest_path = argv[1];
}
else if (argc != 1)
{
sotest_show_usage(argv[0]);
return EXIT_FAILURE;
}
#endif

/* Set the shared library symbol table */

ret = dlsymtab((FAR struct symtab_s *)g_sot_exports, g_sot_nexports);
Expand All @@ -127,6 +178,8 @@ int main(int argc, FAR char *argv[])
}

#ifdef CONFIG_EXAMPLES_SOTEST_BUILTINFS
if (sotest_path == NULL || mount_only)
{
/* Create a ROM disk for the ROMFS filesystem */

desc.minor = 0; /* Minor device number of the ROM disk. */
Expand Down Expand Up @@ -168,28 +221,48 @@ int main(int argc, FAR char *argv[])
devname, BINDIR, strerror(errno));
exit(EXIT_FAILURE);
}

mounted_builtinfs = true;
}
#endif /* CONFIG_EXAMPLES_SOTEST_BUILTINFS */

if (mount_only)
{
return EXIT_SUCCESS;
}

#if CONFIG_LIBC_ELF_MAXDEPEND > 0
if (modprint_path == NULL)
{
modprint_path = BINDIR "/modprint";
}
#endif

if (sotest_path == NULL)
{
sotest_path = BINDIR "/sotest";
}

#if CONFIG_LIBC_ELF_MAXDEPEND > 0
/* Install the first test shared library. The first shared library only
* verifies that symbols exported by one shared library can be used to
* resolve undefined symbols in a second shared library.
*/

handle1 = dlopen(BINDIR "/modprint", RTLD_NOW | RTLD_LOCAL);
handle1 = dlopen(modprint_path, RTLD_NOW | RTLD_LOCAL);
if (handle1 == NULL)
{
fprintf(stderr, "ERROR: dlopen(%s/modprint) failed\n", BINDIR);
fprintf(stderr, "ERROR: dlopen(%s) failed\n", modprint_path);
exit(EXIT_FAILURE);
}
#endif

/* Install the second test shared library */

handle2 = dlopen(BINDIR "/sotest", RTLD_NOW | RTLD_LOCAL);
handle2 = dlopen(sotest_path, RTLD_NOW | RTLD_LOCAL);
if (handle2 == NULL)
{
fprintf(stderr, "ERROR: dlopen(%s/sotest) failed\n", BINDIR);
fprintf(stderr, "ERROR: dlopen(%s) failed\n", sotest_path);
exit(EXIT_FAILURE);
}

Expand Down Expand Up @@ -288,15 +361,18 @@ int main(int argc, FAR char *argv[])
#endif

#ifdef CONFIG_EXAMPLES_SOTEST_BUILTINFS
ret = umount(BINDIR);
if (ret < 0)
if (mounted_builtinfs)
{
fprintf(stderr, "ERROR: umount(%s) failed: %d\n",
BINDIR, errno);
exit(EXIT_FAILURE);
}
ret = umount(BINDIR);
if (ret < 0)
{
fprintf(stderr, "ERROR: umount(%s) failed: %d\n",
BINDIR, errno);
exit(EXIT_FAILURE);
}

unlink(devname);
unlink(devname);
}
#endif /* CONFIG_EXAMPLES_SOTEST_BUILTINFS */

return EXIT_SUCCESS;
Expand Down
Loading