blob: d0bc7b8d7b760e4f742509bb535eb07c8f3c128c [file] [log] [blame]
Andrew Sculle184c5b2022-05-30 10:00:09 +00001/* SPDX-License-Identifier: GPL-2.0+ */
2/*
3 * Copyright (c) 2022 Google, Inc.
4 * Written by Andrew Scull <ascull@google.com>
5 */
6
7#include <command.h>
Tom Riniabb9a042024-05-18 20:20:43 -06008#include <common.h>
Andrew Sculle184c5b2022-05-30 10:00:09 +00009#include <dm.h>
10#include <fuzzing_engine.h>
11#include <test/fuzz.h>
12
13static struct fuzz_test *find_fuzz_test(const char *name)
14{
15 struct fuzz_test *fuzzer = FUZZ_TEST_START();
16 size_t count = FUZZ_TEST_COUNT();
17 size_t i;
18
19 for (i = 0; i < count; ++i) {
20 if (strcmp(name, fuzzer->name) == 0)
21 return fuzzer;
22 ++fuzzer;
23 }
24
25 return NULL;
26}
27
28static struct udevice *find_fuzzing_engine(void)
29{
30 struct udevice *dev;
31
Michal Suchanekac12a2f2022-10-12 21:57:59 +020032 if (uclass_first_device_err(UCLASS_FUZZING_ENGINE, &dev))
Andrew Sculle184c5b2022-05-30 10:00:09 +000033 return NULL;
34
35 return dev;
36}
37
38static int do_fuzz(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[])
39{
40 struct fuzz_test *fuzzer;
41 struct udevice *dev;
42
43 if (argc != 2)
44 return CMD_RET_USAGE;
45
46 fuzzer = find_fuzz_test(argv[1]);
47 if (!fuzzer) {
48 printf("Could not find fuzzer: %s\n", argv[1]);
49 return 1;
50 }
51
52 dev = find_fuzzing_engine();
53 if (!dev) {
54 puts("No fuzzing engine available\n");
55 return 1;
56 }
57
58 while (1) {
59 const uint8_t *data;
60 size_t size;
61
62 if (dm_fuzzing_engine_get_input(dev, &data, &size)) {
63 puts("Fuzzing engine failed\n");
64 return 1;
65 }
66
67 fuzzer->func(data, size);
68 }
69
70 return 1;
71}
72
Tom Rini03f146c2023-10-07 15:13:08 -040073U_BOOT_LONGHELP(fuzz,
74 "[fuzz-test-name] - execute the named fuzz test\n");
Andrew Sculle184c5b2022-05-30 10:00:09 +000075
76U_BOOT_CMD(
77 fuzz, CONFIG_SYS_MAXARGS, 1, do_fuzz,
78 "fuzz tests", fuzz_help_text
79);