[LTP] [PATCH v6 4/4] fw_load: add fw_load02 for custom firmware path
Cyril Hrubis
chrubis@suse.cz
Mon Jun 15 13:49:27 CEST 2026
Hi!
> +static struct fw_data {
> + char file[PATH_MAX];
> + char name[MAX_NAME];
> + int fake;
> +} firmware[FW_NUM];
> +
> +static void create_firmware(const char *dir)
> +{
> + struct fw_data *fw = &firmware[fw_count];
> + char buf[FW_SIZE];
> + int fd;
> +
> + snprintf(fw->name, sizeof(fw->name), "n%d_%s", fw_count, FW_NAME);
> + snprintf(fw->file, sizeof(fw->file), "%s/n%d_%s", dir, fw_count, FW_NAME);
> + memset(buf, fw_count, FW_SIZE);
> +
> + fd = SAFE_OPEN(fw->file, O_WRONLY | O_CREAT | O_TRUNC, 0644);
> + SAFE_WRITE(SAFE_WRITE_ALL, fd, buf, FW_SIZE);
> + SAFE_CLOSE(fd);
> +
> + fw_count++;
> +}
> +
> +static void run(void)
> +{
> + struct fw_data *fw;
> + int result = 0;
> + int fail;
> +
> + SAFE_FILE_PRINTF(DEV_FWNUM, "%d", fw_count);
> + SAFE_FILE_SCANF(DEV_RESULT, "%d", &result);
> +
> + for (int i = 0; i < fw_count; i++) {
> + fw = &firmware[i];
> +
> + if (fw->fake) {
> + tst_res(result & (1 << i) ? TFAIL : TPASS,
> + "Firmware '%s' correctly not loaded", fw->name);
> + } else {
> + fail = (result & (1 << i)) == 0;
> + tst_res(fail ? TFAIL : TPASS,
> + "Firmware '%s' loaded", fw->name);
> + }
> + }
> +}
These functions are nearly identical to the fw_load01.c maybe we should
put them into a header and include it into both tests instead?
I guess that we can have a static inline function do_test() that would
take the fw_data and fw_count as an argument and call it from run(). The
same for create_firmare(). We can pas sthe fw_data pointer that would be
initialized instead of acting on globals and put it into the shared
header.
> +static void setup(void)
> +{
> + char fw_size_param[32];
> + char *tmpdir = tst_tmpdir_path();
> +
> + tst_requires_module_signature_disabled();
> +
> + SAFE_FILE_PRINTF(FW_PATH, "%s", tmpdir);
> +
> + snprintf(fw_size_param, sizeof(fw_size_param), "fw_size=%d", FW_SIZE);
> + char *const mod_params[] = {fw_size_param, NULL};
> +
> + tst_module_load(MNAME_KO, mod_params);
> + module_loaded = 1;
> +
> + for (int i = 0; i < FW_NUM - 1; i++)
> + create_firmware(tmpdir);
> +
> + /* add a fake file that is never created on disk */
> + snprintf(firmware[fw_count].name, sizeof(firmware[fw_count].name),
> + "n%d_%s", fw_count, FW_NAME);
> + firmware[fw_count].fake = 1;
> + fw_count++;
And most of the setup code can be shared as well.
> +}
> +
> +static void cleanup(void)
> +{
> + if (module_loaded)
> + tst_module_unload(MNAME_KO);
> +}
> +
> +static struct tst_test test = {
> + .test_all = run,
> + .setup = setup,
> + .cleanup = cleanup,
> + .needs_root = 1,
> + .needs_tmpdir = 1,
> + .needs_kconfigs = (const char *[]) {
> + "CONFIG_FW_LOADER=y|CONFIG_FW_LOADER=m",
> + NULL,
> + },
> + .save_restore = (const struct tst_path_val[]) {
> + {FW_PATH, NULL, TST_SR_TCONF},
> + {},
> + },
> +};
>
> --
> 2.51.0
>
>
> --
> Mailing list info: https://lists.linux.it/listinfo/ltp
--
Cyril Hrubis
chrubis@suse.cz
More information about the ltp
mailing list