blob: 8aefd41b4b6c91d28f03dc0a4dfe77503941725b [file] [log] [blame]
rjw1f884582022-01-06 17:20:42 +08001/*
2 * Copyright (c) 2019 MediaTek Inc.
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining
5 * a copy of this software and associated documentation files
6 * (the "Software"), to deal in the Software without restriction,
7 * including without limitation the rights to use, copy, modify, merge,
8 * publish, distribute, sublicense, and/or sell copies of the Software,
9 * and to permit persons to whom the Software is furnished to do so,
10 * subject to the following conditions:
11 *
12 * The above copyright notice and this permission notice shall be
13 * included in all copies or substantial portions of the Software.
14 *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
16 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
17 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
18 * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
19 * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
20 * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
21 * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
22 */
23
24#include <assert.h>
25#include <debug.h>
26#include <err.h>
27#include <fit.h>
28#include <lib/console.h>
29#include <lib/kcmdline.h>
30#include <lib/mempool.h>
31#include <libfdt.h>
32#include <malloc.h>
33#include <stddef.h>
34#include <stdio.h>
35#include <stdlib.h>
36#include <string.h>
37
38#if WITH_LIB_CKSUM
39#include <lib/cksum.h>
40#endif
41
42#if defined(WITH_LIB_CONSOLE)
43
44#if LK_DEBUGLEVEL > 0
45static int cmd_kcmdline(int argc, const cmd_args *argv);
46
47STATIC_COMMAND_START
48STATIC_COMMAND("kcmdline", "kcmdline debug commands", &cmd_kcmdline)
49STATIC_COMMAND_END(kcmdline);
50
51static int cmd_kcmdline(int argc, const cmd_args *argv)
52{
53 int rc = 0;
54
55 if (argc < 2) {
56notenoughargs:
57 printf("not enough arguments:\n");
58usage:
59 printf("%s init - init kcmdline\n", argv[0].str);
60 printf("%s print - print kcmdline buffer\n", argv[0].str);
61 printf("%s append <append_arg>\n", argv[0].str);
62 printf("%s subst <old_arg> <new_arg>\n", argv[0].str);
63 printf("%s finalized <part_name> - write buffer to <part_name> dtb\n",
64 argv[0].str);
65 return -1;
66 }
67
68 if (!strcmp(argv[1].str, "init")) {
69 if ((rc = kcmdline_init()) != NO_ERROR) {
70 printf("kcmdline init failed, rc=%d\n", rc);
71 return -1;
72 }
73 } else if (!strcmp(argv[1].str, "print")) {
74 kcmdline_print();
75 } else if (!strcmp(argv[1].str, "append")) {
76 if ((rc = kcmdline_append(argv[2].str)) != NO_ERROR) {
77 printf("kcmdline append %s failed, rc=%d\n", argv[2].str, rc);
78 return -1;
79 }
80 } else if (!strcmp(argv[1].str, "subst")) {
81 if ((rc = kcmdline_subst(argv[2].str, argv[3].str)) != NO_ERROR) {
82 printf("kcmdline subst failed, old: %s, new: %s, rc=%d\n",
83 argv[2].str, argv[3].str, rc);
84 return -1;
85 }
86 } else if (!strcmp(argv[1].str, "finalized")) {
87 void *fit;
88 addr_t load;
89
90 rc = fit_get_image(argv[2].str, &fit);
91 if (rc < 0) {
92 printf("fit_get_image: %s failed, rc=%d\n", argv[2].str, rc);
93 return -1;
94 }
95 rc = fit_load_image(NULL, "fdt", fit, &load, NULL, NULL, false);
96 if (rc < 0) {
97 printf("fit_load_image: load fdt failed, rc=%d\n", rc);
98 return -1;
99 }
100 hexdump((const void *)load, 512);
101
102 if ((rc = kcmdline_finalized((void *)load, 0x200000)) != NO_ERROR) {
103 printf("kcmdline finalized failed, rc=%d\n", rc);
104 return -1;
105 }
106 } else {
107 printf("unrecognized subcommand\n");
108 goto usage;
109 }
110
111 return rc;
112}
113#endif
114#endif