fdt_loader_cmd.c revision 265066
1208538Sraj/*-
2208538Sraj * Copyright (c) 2009-2010 The FreeBSD Foundation
3208538Sraj * All rights reserved.
4208538Sraj *
5208538Sraj * This software was developed by Semihalf under sponsorship from
6208538Sraj * the FreeBSD Foundation.
7208538Sraj *
8208538Sraj * Redistribution and use in source and binary forms, with or without
9208538Sraj * modification, are permitted provided that the following conditions
10208538Sraj * are met:
11208538Sraj * 1. Redistributions of source code must retain the above copyright
12208538Sraj *    notice, this list of conditions and the following disclaimer.
13208538Sraj * 2. Redistributions in binary form must reproduce the above copyright
14208538Sraj *    notice, this list of conditions and the following disclaimer in the
15208538Sraj *    documentation and/or other materials provided with the distribution.
16208538Sraj *
17208538Sraj * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
18208538Sraj * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19208538Sraj * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20208538Sraj * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
21208538Sraj * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22208538Sraj * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23208538Sraj * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24208538Sraj * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25208538Sraj * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26208538Sraj * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27208538Sraj * SUCH DAMAGE.
28208538Sraj */
29208538Sraj
30208538Sraj#include <sys/cdefs.h>
31208538Sraj__FBSDID("$FreeBSD: stable/10/sys/boot/fdt/fdt_loader_cmd.c 265066 2014-04-29 00:13:25Z ian $");
32208538Sraj
33208538Sraj#include <stand.h>
34208538Sraj#include <fdt.h>
35208538Sraj#include <libfdt.h>
36233230Sraj#include <sys/param.h>
37233230Sraj#include <sys/linker.h>
38233230Sraj#include <machine/elf.h>
39208538Sraj
40208538Sraj#include "bootstrap.h"
41208538Sraj#include "glue.h"
42208538Sraj
43208538Sraj#ifdef DEBUG
44208538Sraj#define debugf(fmt, args...) do { printf("%s(): ", __func__);	\
45208538Sraj    printf(fmt,##args); } while (0)
46208538Sraj#else
47208538Sraj#define debugf(fmt, args...)
48208538Sraj#endif
49208538Sraj
50208538Sraj#define FDT_CWD_LEN	256
51208538Sraj#define FDT_MAX_DEPTH	6
52208538Sraj
53208538Sraj#define FDT_PROP_SEP	" = "
54208538Sraj
55208538Sraj#define STR(number) #number
56208538Sraj#define STRINGIFY(number) STR(number)
57208538Sraj
58235529Skientzle#define COPYOUT(s,d,l)	archsw.arch_copyout(s, d, l)
59235529Skientzle#define COPYIN(s,d,l)	archsw.arch_copyin(s, d, l)
60233230Sraj
61233230Sraj#define FDT_STATIC_DTB_SYMBOL	"fdt_static_dtb"
62233230Sraj
63243693Sgonzo#define	CMD_REQUIRES_BLOB	0x01
64243693Sgonzo
65247201Skientzle/* Location of FDT yet to be loaded. */
66247250Skientzle/* This may be in read-only memory, so can't be manipulated directly. */
67247201Skientzlestatic struct fdt_header *fdt_to_load = NULL;
68247250Skientzle/* Location of FDT on heap. */
69247250Skientzle/* This is the copy we actually manipulate. */
70208538Srajstatic struct fdt_header *fdtp = NULL;
71235529Skientzle/* Size of FDT blob */
72235529Skientzlestatic size_t fdtp_size = 0;
73247250Skientzle/* Location of FDT in kernel or module. */
74247250Skientzle/* This won't be set if FDT is loaded from disk or memory. */
75247250Skientzle/* If it is set, we'll update it when fdt_copy() gets called. */
76235529Skientzlestatic vm_offset_t fdtp_va = 0;
77208538Sraj
78243693Sgonzostatic int fdt_load_dtb(vm_offset_t va);
79243693Sgonzo
80208538Srajstatic int fdt_cmd_nyi(int argc, char *argv[]);
81208538Sraj
82243693Sgonzostatic int fdt_cmd_addr(int argc, char *argv[]);
83208538Srajstatic int fdt_cmd_mkprop(int argc, char *argv[]);
84208538Srajstatic int fdt_cmd_cd(int argc, char *argv[]);
85208538Srajstatic int fdt_cmd_hdr(int argc, char *argv[]);
86208538Srajstatic int fdt_cmd_ls(int argc, char *argv[]);
87208538Srajstatic int fdt_cmd_prop(int argc, char *argv[]);
88208538Srajstatic int fdt_cmd_pwd(int argc, char *argv[]);
89208538Srajstatic int fdt_cmd_rm(int argc, char *argv[]);
90208538Srajstatic int fdt_cmd_mknode(int argc, char *argv[]);
91243693Sgonzostatic int fdt_cmd_mres(int argc, char *argv[]);
92208538Sraj
93208538Srajtypedef int cmdf_t(int, char *[]);
94208538Sraj
95208538Srajstruct cmdtab {
96208538Sraj	char	*name;
97208538Sraj	cmdf_t	*handler;
98243693Sgonzo	int	flags;
99208538Sraj};
100208538Sraj
101208538Srajstatic const struct cmdtab commands[] = {
102243693Sgonzo	{ "addr", &fdt_cmd_addr,	0 },
103243693Sgonzo	{ "alias", &fdt_cmd_nyi,	0 },
104243693Sgonzo	{ "cd", &fdt_cmd_cd,		CMD_REQUIRES_BLOB },
105243693Sgonzo	{ "header", &fdt_cmd_hdr,	CMD_REQUIRES_BLOB },
106243693Sgonzo	{ "ls", &fdt_cmd_ls,		CMD_REQUIRES_BLOB },
107243693Sgonzo	{ "mknode", &fdt_cmd_mknode,	CMD_REQUIRES_BLOB },
108243693Sgonzo	{ "mkprop", &fdt_cmd_mkprop,	CMD_REQUIRES_BLOB },
109243693Sgonzo	{ "mres", &fdt_cmd_mres,	CMD_REQUIRES_BLOB },
110243693Sgonzo	{ "prop", &fdt_cmd_prop,	CMD_REQUIRES_BLOB },
111243693Sgonzo	{ "pwd", &fdt_cmd_pwd,		CMD_REQUIRES_BLOB },
112243693Sgonzo	{ "rm", &fdt_cmd_rm,		CMD_REQUIRES_BLOB },
113208538Sraj	{ NULL, NULL }
114208538Sraj};
115208538Sraj
116208538Srajstatic char cwd[FDT_CWD_LEN] = "/";
117208538Sraj
118233230Srajstatic vm_offset_t
119235529Skientzlefdt_find_static_dtb()
120233230Sraj{
121248121Sian	Elf_Ehdr *ehdr;
122248121Sian	Elf_Shdr *shdr;
123233230Sraj	Elf_Sym sym;
124248121Sian	vm_offset_t strtab, symtab, fdt_start;
125233230Sraj	uint64_t offs;
126233230Sraj	struct preloaded_file *kfp;
127233230Sraj	struct file_metadata *md;
128235529Skientzle	char *strp;
129248121Sian	int i, sym_count;
130233230Sraj
131248934Skientzle	sym_count = symtab = strtab = 0;
132235529Skientzle	strp = NULL;
133233230Sraj
134233230Sraj	offs = __elfN(relocation_offset);
135233230Sraj
136233230Sraj	kfp = file_findfile(NULL, NULL);
137233230Sraj	if (kfp == NULL)
138233230Sraj		return (0);
139233230Sraj
140248121Sian	/* Locate the dynamic symbols and strtab. */
141248121Sian	md = file_findmetadata(kfp, MODINFOMD_ELFHDR);
142233230Sraj	if (md == NULL)
143233230Sraj		return (0);
144248121Sian	ehdr = (Elf_Ehdr *)md->md_data;
145233230Sraj
146248121Sian	md = file_findmetadata(kfp, MODINFOMD_SHDR);
147233230Sraj	if (md == NULL)
148233230Sraj		return (0);
149248121Sian	shdr = (Elf_Shdr *)md->md_data;
150233230Sraj
151248121Sian	for (i = 0; i < ehdr->e_shnum; ++i) {
152248121Sian		if (shdr[i].sh_type == SHT_DYNSYM && symtab == 0) {
153248121Sian			symtab = shdr[i].sh_addr + offs;
154248121Sian			sym_count = shdr[i].sh_size / sizeof(Elf_Sym);
155248121Sian		} else if (shdr[i].sh_type == SHT_STRTAB && strtab == 0) {
156248121Sian			strtab = shdr[i].sh_addr + offs;
157233230Sraj		}
158233230Sraj	}
159233230Sraj
160233230Sraj	/*
161233230Sraj	 * The most efficent way to find a symbol would be to calculate a
162233230Sraj	 * hash, find proper bucket and chain, and thus find a symbol.
163233230Sraj	 * However, that would involve code duplication (e.g. for hash
164233230Sraj	 * function). So we're using simpler and a bit slower way: we're
165233230Sraj	 * iterating through symbols, searching for the one which name is
166233230Sraj	 * 'equal' to 'fdt_static_dtb'. To speed up the process a little bit,
167233230Sraj	 * we are eliminating symbols type of which is not STT_NOTYPE, or(and)
168233230Sraj	 * those which binding attribute is not STB_GLOBAL.
169233230Sraj	 */
170235529Skientzle	fdt_start = 0;
171235529Skientzle	while (sym_count > 0 && fdt_start == 0) {
172235529Skientzle		COPYOUT(symtab, &sym, sizeof(sym));
173235529Skientzle		symtab += sizeof(sym);
174235529Skientzle		--sym_count;
175233230Sraj		if (ELF_ST_BIND(sym.st_info) != STB_GLOBAL ||
176233230Sraj		    ELF_ST_TYPE(sym.st_info) != STT_NOTYPE)
177233230Sraj			continue;
178235529Skientzle		strp = strdupout(strtab + sym.st_name);
179235529Skientzle		if (strcmp(strp, FDT_STATIC_DTB_SYMBOL) == 0)
180235529Skientzle			fdt_start = (vm_offset_t)sym.st_value + offs;
181233230Sraj		free(strp);
182233230Sraj	}
183235529Skientzle	return (fdt_start);
184233230Sraj}
185233230Sraj
186208538Srajstatic int
187243693Sgonzofdt_load_dtb(vm_offset_t va)
188208538Sraj{
189235529Skientzle	struct fdt_header header;
190208538Sraj	int err;
191208538Sraj
192243693Sgonzo	COPYOUT(va, &header, sizeof(header));
193243693Sgonzo	err = fdt_check_header(&header);
194243693Sgonzo	if (err < 0) {
195243693Sgonzo		if (err == -FDT_ERR_BADVERSION)
196243693Sgonzo			sprintf(command_errbuf,
197243693Sgonzo			    "incompatible blob version: %d, should be: %d",
198243693Sgonzo			    fdt_version(fdtp), FDT_LAST_SUPPORTED_VERSION);
199243693Sgonzo
200243693Sgonzo		else
201243693Sgonzo			sprintf(command_errbuf, "error validating blob: %s",
202243693Sgonzo			    fdt_strerror(err));
203243693Sgonzo		return (1);
204243693Sgonzo	}
205243693Sgonzo
206208538Sraj	/*
207243693Sgonzo	 * Release previous blob
208208538Sraj	 */
209243693Sgonzo	if (fdtp)
210243693Sgonzo		free(fdtp);
211208538Sraj
212235529Skientzle	fdtp_size = fdt_totalsize(&header);
213235529Skientzle	fdtp = malloc(fdtp_size);
214243693Sgonzo
215235529Skientzle	if (fdtp == NULL) {
216235529Skientzle		command_errmsg = "can't allocate memory for device tree copy";
217243693Sgonzo		return (1);
218235529Skientzle	}
219235529Skientzle
220243693Sgonzo	fdtp_va = va;
221243693Sgonzo	COPYOUT(va, fdtp, fdtp_size);
222243693Sgonzo	debugf("DTB blob found at 0x%jx, size: 0x%jx\n", (uintmax_t)va, (uintmax_t)fdtp_size);
223208538Sraj
224243693Sgonzo	return (0);
225243693Sgonzo}
226243693Sgonzo
227243693Sgonzostatic int
228247045Skientzlefdt_load_dtb_addr(struct fdt_header *header)
229243693Sgonzo{
230265065Sian	int err;
231243693Sgonzo
232247250Skientzle	fdtp_size = fdt_totalsize(header);
233265065Sian	err = fdt_check_header(header);
234265065Sian	if (err < 0) {
235265065Sian		sprintf(command_errbuf, "error validating blob: %s",
236265065Sian		    fdt_strerror(err));
237265065Sian		return (err);
238265065Sian	}
239247250Skientzle	free(fdtp);
240247250Skientzle	if ((fdtp = malloc(fdtp_size)) == NULL) {
241247250Skientzle		command_errmsg = "can't allocate memory for device tree copy";
242247045Skientzle		return (1);
243208538Sraj	}
244247250Skientzle
245247250Skientzle	fdtp_va = 0; // Don't write this back into module or kernel.
246247250Skientzle	bcopy(header, fdtp, fdtp_size);
247247250Skientzle	return (0);
248247045Skientzle}
249243693Sgonzo
250247045Skientzlestatic int
251247045Skientzlefdt_setup_fdtp()
252247045Skientzle{
253265066Sian	struct preloaded_file *bfp;
254265066Sian	struct fdt_header *hdr;
255265066Sian	const char *s;
256265066Sian	char *p;
257265066Sian	vm_offset_t va;
258265066Sian
259265066Sian	if ((bfp = file_findfile(NULL, "dtb")) != NULL) {
260265066Sian		printf("Using DTB from loaded file.\n");
261265066Sian		return fdt_load_dtb(bfp->f_addr);
262265066Sian	}
263265066Sian
264265066Sian	if (fdt_to_load != NULL) {
265265066Sian		printf("Using DTB from memory address 0x%08X.\n",
266265066Sian		    (unsigned int)fdt_to_load);
267265066Sian		return fdt_load_dtb_addr(fdt_to_load);
268265066Sian	}
269247045Skientzle
270265066Sian	/* Board vendors use both fdtaddr and fdt_addr names.  Grrrr. */
271265066Sian	s = ub_env_get("fdtaddr");
272265066Sian	if (s == NULL)
273265066Sian		s = ub_env_get("fdt_addr");
274265066Sian	if (s != NULL && *s != '\0') {
275265066Sian		hdr = (struct fdt_header *)strtoul(s, &p, 16);
276265066Sian		if (*p == '\0') {
277265066Sian			printf("Using DTB provided by U-Boot.\n");
278265066Sian			return fdt_load_dtb_addr(hdr);
279265066Sian		}
280265066Sian	}
281265066Sian
282265066Sian	if ((va = fdt_find_static_dtb()) != 0) {
283265066Sian		printf("Using DTB compiled into kernel.\n");
284265066Sian		return (fdt_load_dtb(va));
285265066Sian	}
286265066Sian
287265066Sian	command_errmsg = "no device tree blob found!";
288265066Sian	return (1);
289208538Sraj}
290208538Sraj
291208538Sraj#define fdt_strtovect(str, cellbuf, lim, cellsize) _fdt_strtovect((str), \
292208538Sraj    (cellbuf), (lim), (cellsize), 0);
293208538Sraj
294208538Sraj/* Force using base 16 */
295208538Sraj#define fdt_strtovectx(str, cellbuf, lim, cellsize) _fdt_strtovect((str), \
296208538Sraj    (cellbuf), (lim), (cellsize), 16);
297208538Sraj
298208538Srajstatic int
299208538Sraj_fdt_strtovect(char *str, void *cellbuf, int lim, unsigned char cellsize,
300208538Sraj    uint8_t base)
301208538Sraj{
302208538Sraj	char *buf = str;
303208538Sraj	char *end = str + strlen(str) - 2;
304208538Sraj	uint32_t *u32buf = NULL;
305208538Sraj	uint8_t *u8buf = NULL;
306208538Sraj	int cnt = 0;
307208538Sraj
308208538Sraj	if (cellsize == sizeof(uint32_t))
309208538Sraj		u32buf = (uint32_t *)cellbuf;
310208538Sraj	else
311208538Sraj		u8buf = (uint8_t *)cellbuf;
312208538Sraj
313208538Sraj	if (lim == 0)
314208538Sraj		return (0);
315208538Sraj
316208538Sraj	while (buf < end) {
317208538Sraj
318208538Sraj		/* Skip white whitespace(s)/separators */
319208538Sraj		while (!isxdigit(*buf) && buf < end)
320208538Sraj			buf++;
321208538Sraj
322208538Sraj		if (u32buf != NULL)
323208538Sraj			u32buf[cnt] =
324208538Sraj			    cpu_to_fdt32((uint32_t)strtol(buf, NULL, base));
325208538Sraj
326208538Sraj		else
327208538Sraj			u8buf[cnt] = (uint8_t)strtol(buf, NULL, base);
328208538Sraj
329208538Sraj		if (cnt + 1 <= lim - 1)
330208538Sraj			cnt++;
331208538Sraj		else
332208538Sraj			break;
333208538Sraj		buf++;
334208538Sraj		/* Find another number */
335208538Sraj		while ((isxdigit(*buf) || *buf == 'x') && buf < end)
336208538Sraj			buf++;
337208538Sraj	}
338208538Sraj	return (cnt);
339208538Sraj}
340208538Sraj
341208538Sraj#define	TMP_MAX_ETH	8
342208538Sraj
343247250Skientzlestatic void
344208538Srajfixup_ethernet(const char *env, char *ethstr, int *eth_no, int len)
345208538Sraj{
346208538Sraj	char *end, *str;
347208538Sraj	uint8_t tmp_addr[6];
348208538Sraj	int i, n;
349208538Sraj
350208538Sraj	/* Extract interface number */
351208538Sraj	i = strtol(env + 3, &end, 10);
352208538Sraj	if (end == (env + 3))
353208538Sraj		/* 'ethaddr' means interface 0 address */
354208538Sraj		n = 0;
355208538Sraj	else
356208538Sraj		n = i;
357208538Sraj
358208538Sraj	if (n > TMP_MAX_ETH)
359208538Sraj		return;
360208538Sraj
361208538Sraj	str = ub_env_get(env);
362208538Sraj
363208538Sraj	/* Convert macaddr string into a vector of uints */
364208538Sraj	fdt_strtovectx(str, &tmp_addr, 6, sizeof(uint8_t));
365208538Sraj	if (n != 0) {
366208538Sraj		i = strlen(env) - 7;
367208538Sraj		strncpy(ethstr + 8, env + 3, i);
368208538Sraj	}
369208538Sraj	/* Set actual property to a value from vect */
370208538Sraj	fdt_setprop(fdtp, fdt_path_offset(fdtp, ethstr),
371208538Sraj	    "local-mac-address", &tmp_addr, 6 * sizeof(uint8_t));
372208538Sraj
373208538Sraj	/* Clear ethernet..XXXX.. string */
374208538Sraj	bzero(ethstr + 8, len - 8);
375208538Sraj
376208538Sraj	if (n + 1 > *eth_no)
377208538Sraj		*eth_no = n + 1;
378208538Sraj}
379208538Sraj
380247250Skientzlestatic void
381208538Srajfixup_cpubusfreqs(unsigned long cpufreq, unsigned long busfreq)
382208538Sraj{
383208538Sraj	int lo, o = 0, o2, maxo = 0, depth;
384208538Sraj	const uint32_t zero = 0;
385208538Sraj
386208538Sraj	/* We want to modify every subnode of /cpus */
387208538Sraj	o = fdt_path_offset(fdtp, "/cpus");
388235261Skientzle	if (o < 0)
389235261Skientzle		return;
390208538Sraj
391208538Sraj	/* maxo should contain offset of node next to /cpus */
392208538Sraj	depth = 0;
393208538Sraj	maxo = o;
394208538Sraj	while (depth != -1)
395208538Sraj		maxo = fdt_next_node(fdtp, maxo, &depth);
396208538Sraj
397208538Sraj	/* Find CPU frequency properties */
398208538Sraj	o = fdt_node_offset_by_prop_value(fdtp, o, "clock-frequency",
399208538Sraj	    &zero, sizeof(uint32_t));
400208538Sraj
401208538Sraj	o2 = fdt_node_offset_by_prop_value(fdtp, o, "bus-frequency", &zero,
402208538Sraj	    sizeof(uint32_t));
403208538Sraj
404208538Sraj	lo = MIN(o, o2);
405208538Sraj
406208538Sraj	while (o != -FDT_ERR_NOTFOUND && o2 != -FDT_ERR_NOTFOUND) {
407208538Sraj
408208538Sraj		o = fdt_node_offset_by_prop_value(fdtp, lo,
409208538Sraj		    "clock-frequency", &zero, sizeof(uint32_t));
410208538Sraj
411208538Sraj		o2 = fdt_node_offset_by_prop_value(fdtp, lo, "bus-frequency",
412208538Sraj		    &zero, sizeof(uint32_t));
413208538Sraj
414208538Sraj		/* We're only interested in /cpus subnode(s) */
415208538Sraj		if (lo > maxo)
416208538Sraj			break;
417208538Sraj
418208538Sraj		fdt_setprop_inplace_cell(fdtp, lo, "clock-frequency",
419208538Sraj		    (uint32_t)cpufreq);
420208538Sraj
421208538Sraj		fdt_setprop_inplace_cell(fdtp, lo, "bus-frequency",
422208538Sraj		    (uint32_t)busfreq);
423208538Sraj
424208538Sraj		lo = MIN(o, o2);
425208538Sraj	}
426208538Sraj}
427208538Sraj
428247250Skientzlestatic int
429208538Srajfdt_reg_valid(uint32_t *reg, int len, int addr_cells, int size_cells)
430208538Sraj{
431208538Sraj	int cells_in_tuple, i, tuples, tuple_size;
432208538Sraj	uint32_t cur_start, cur_size;
433208538Sraj
434208538Sraj	cells_in_tuple = (addr_cells + size_cells);
435208538Sraj	tuple_size = cells_in_tuple * sizeof(uint32_t);
436208538Sraj	tuples = len / tuple_size;
437208538Sraj	if (tuples == 0)
438208538Sraj		return (EINVAL);
439208538Sraj
440208538Sraj	for (i = 0; i < tuples; i++) {
441208538Sraj		if (addr_cells == 2)
442208538Sraj			cur_start = fdt64_to_cpu(reg[i * cells_in_tuple]);
443208538Sraj		else
444208538Sraj			cur_start = fdt32_to_cpu(reg[i * cells_in_tuple]);
445208538Sraj
446208538Sraj		if (size_cells == 2)
447208538Sraj			cur_size = fdt64_to_cpu(reg[i * cells_in_tuple + 2]);
448208538Sraj		else
449208538Sraj			cur_size = fdt32_to_cpu(reg[i * cells_in_tuple + 1]);
450208538Sraj
451208538Sraj		if (cur_size == 0)
452208538Sraj			return (EINVAL);
453208538Sraj
454208538Sraj		debugf(" reg#%d (start: 0x%0x size: 0x%0x) valid!\n",
455208538Sraj		    i, cur_start, cur_size);
456208538Sraj	}
457208538Sraj	return (0);
458208538Sraj}
459208538Sraj
460247250Skientzlestatic void
461208538Srajfixup_memory(struct sys_info *si)
462208538Sraj{
463208538Sraj	struct mem_region *curmr;
464208538Sraj	uint32_t addr_cells, size_cells;
465208538Sraj	uint32_t *addr_cellsp, *reg,  *size_cellsp;
466208538Sraj	int err, i, len, memory, realmrno, root;
467208538Sraj	uint8_t *buf, *sb;
468243693Sgonzo	uint64_t rstart, rsize;
469243693Sgonzo	int reserved;
470208538Sraj
471208538Sraj	root = fdt_path_offset(fdtp, "/");
472208538Sraj	if (root < 0) {
473208538Sraj		sprintf(command_errbuf, "Could not find root node !");
474208538Sraj		return;
475208538Sraj	}
476208538Sraj
477208538Sraj	memory = fdt_path_offset(fdtp, "/memory");
478208538Sraj	if (memory <= 0) {
479208538Sraj		/* Create proper '/memory' node. */
480208538Sraj		memory = fdt_add_subnode(fdtp, root, "memory");
481208538Sraj		if (memory <= 0) {
482208538Sraj			sprintf(command_errbuf, "Could not fixup '/memory' "
483208538Sraj			    "node, error code : %d!\n", memory);
484208538Sraj			return;
485208538Sraj		}
486208538Sraj
487208538Sraj		err = fdt_setprop(fdtp, memory, "device_type", "memory",
488208538Sraj		    sizeof("memory"));
489208538Sraj
490208538Sraj		if (err < 0)
491208538Sraj			return;
492208538Sraj	}
493208538Sraj
494208538Sraj	addr_cellsp = (uint32_t *)fdt_getprop(fdtp, root, "#address-cells",
495208538Sraj	    NULL);
496208538Sraj	size_cellsp = (uint32_t *)fdt_getprop(fdtp, root, "#size-cells", NULL);
497208538Sraj
498208538Sraj	if (addr_cellsp == NULL || size_cellsp == NULL) {
499208538Sraj		sprintf(command_errbuf, "Could not fixup '/memory' node : "
500208538Sraj		    "%s %s property not found in root node!\n",
501208538Sraj		    (!addr_cellsp) ? "#address-cells" : "",
502208538Sraj		    (!size_cellsp) ? "#size-cells" : "");
503208538Sraj		return;
504208538Sraj	}
505208538Sraj
506208538Sraj	addr_cells = fdt32_to_cpu(*addr_cellsp);
507208538Sraj	size_cells = fdt32_to_cpu(*size_cellsp);
508208538Sraj
509243693Sgonzo	/*
510243693Sgonzo	 * Convert memreserve data to memreserve property
511243693Sgonzo	 * Check if property already exists
512243693Sgonzo	 */
513243693Sgonzo	reserved = fdt_num_mem_rsv(fdtp);
514243693Sgonzo	if (reserved &&
515243693Sgonzo	    (fdt_getprop(fdtp, root, "memreserve", NULL) == NULL)) {
516243693Sgonzo		len = (addr_cells + size_cells) * reserved * sizeof(uint32_t);
517243693Sgonzo		sb = buf = (uint8_t *)malloc(len);
518243693Sgonzo		if (!buf)
519243693Sgonzo			return;
520243693Sgonzo
521243693Sgonzo		bzero(buf, len);
522243693Sgonzo
523243693Sgonzo		for (i = 0; i < reserved; i++) {
524243693Sgonzo			curmr = &si->mr[i];
525243693Sgonzo			if (fdt_get_mem_rsv(fdtp, i, &rstart, &rsize))
526243693Sgonzo				break;
527243693Sgonzo			if (rsize) {
528243693Sgonzo				/* Ensure endianess, and put cells into a buffer */
529243693Sgonzo				if (addr_cells == 2)
530243693Sgonzo					*(uint64_t *)buf =
531243693Sgonzo					    cpu_to_fdt64(rstart);
532243693Sgonzo				else
533243693Sgonzo					*(uint32_t *)buf =
534243693Sgonzo					    cpu_to_fdt32(rstart);
535243693Sgonzo
536243693Sgonzo				buf += sizeof(uint32_t) * addr_cells;
537243693Sgonzo				if (size_cells == 2)
538243693Sgonzo					*(uint64_t *)buf =
539243693Sgonzo					    cpu_to_fdt64(rsize);
540243693Sgonzo				else
541243693Sgonzo					*(uint32_t *)buf =
542243693Sgonzo					    cpu_to_fdt32(rsize);
543243693Sgonzo
544243693Sgonzo				buf += sizeof(uint32_t) * size_cells;
545243693Sgonzo			}
546243693Sgonzo		}
547243693Sgonzo
548243693Sgonzo		/* Set property */
549243693Sgonzo		if ((err = fdt_setprop(fdtp, root, "memreserve", sb, len)) < 0)
550243693Sgonzo			printf("Could not fixup 'memreserve' property.\n");
551243693Sgonzo
552243693Sgonzo		free(sb);
553243693Sgonzo	}
554243693Sgonzo
555208538Sraj	/* Count valid memory regions entries in sysinfo. */
556208538Sraj	realmrno = si->mr_no;
557208538Sraj	for (i = 0; i < si->mr_no; i++)
558208538Sraj		if (si->mr[i].start == 0 && si->mr[i].size == 0)
559208538Sraj			realmrno--;
560208538Sraj
561208538Sraj	if (realmrno == 0) {
562208538Sraj		sprintf(command_errbuf, "Could not fixup '/memory' node : "
563208538Sraj		    "sysinfo doesn't contain valid memory regions info!\n");
564208538Sraj		return;
565208538Sraj	}
566208538Sraj
567208538Sraj	if ((reg = (uint32_t *)fdt_getprop(fdtp, memory, "reg",
568208538Sraj	    &len)) != NULL) {
569208538Sraj
570208538Sraj		if (fdt_reg_valid(reg, len, addr_cells, size_cells) == 0)
571208538Sraj			/*
572208538Sraj			 * Do not apply fixup if existing 'reg' property
573208538Sraj			 * seems to be valid.
574208538Sraj			 */
575208538Sraj			return;
576208538Sraj	}
577208538Sraj
578208538Sraj	len = (addr_cells + size_cells) * realmrno * sizeof(uint32_t);
579208538Sraj	sb = buf = (uint8_t *)malloc(len);
580208538Sraj	if (!buf)
581208538Sraj		return;
582208538Sraj
583208538Sraj	bzero(buf, len);
584208538Sraj
585208538Sraj	for (i = 0; i < si->mr_no; i++) {
586208538Sraj		curmr = &si->mr[i];
587208538Sraj		if (curmr->size != 0) {
588208538Sraj			/* Ensure endianess, and put cells into a buffer */
589208538Sraj			if (addr_cells == 2)
590208538Sraj				*(uint64_t *)buf =
591208538Sraj				    cpu_to_fdt64(curmr->start);
592208538Sraj			else
593208538Sraj				*(uint32_t *)buf =
594208538Sraj				    cpu_to_fdt32(curmr->start);
595208538Sraj
596208538Sraj			buf += sizeof(uint32_t) * addr_cells;
597208538Sraj			if (size_cells == 2)
598208538Sraj				*(uint64_t *)buf =
599208538Sraj				    cpu_to_fdt64(curmr->size);
600208538Sraj			else
601208538Sraj				*(uint32_t *)buf =
602208538Sraj				    cpu_to_fdt32(curmr->size);
603208538Sraj
604208538Sraj			buf += sizeof(uint32_t) * size_cells;
605208538Sraj		}
606208538Sraj	}
607208538Sraj
608208538Sraj	/* Set property */
609208538Sraj	if ((err = fdt_setprop(fdtp, memory, "reg", sb, len)) < 0)
610208538Sraj		sprintf(command_errbuf, "Could not fixup '/memory' node.\n");
611243693Sgonzo
612243693Sgonzo	free(sb);
613208538Sraj}
614208538Sraj
615247250Skientzlestatic void
616208538Srajfixup_stdout(const char *env)
617208538Sraj{
618208538Sraj	const char *str;
619208538Sraj	char *ptr;
620208538Sraj	int serialno;
621208538Sraj	int len, no, sero;
622208538Sraj	const struct fdt_property *prop;
623208538Sraj	char *tmp[10];
624208538Sraj
625208538Sraj	str = ub_env_get(env);
626208538Sraj	ptr = (char *)str + strlen(str) - 1;
627208538Sraj	while (ptr > str && isdigit(*(str - 1)))
628208538Sraj		str--;
629208538Sraj
630208538Sraj	if (ptr == str)
631208538Sraj		return;
632208538Sraj
633208538Sraj	serialno = (int)strtol(ptr, NULL, 0);
634208538Sraj	no = fdt_path_offset(fdtp, "/chosen");
635208538Sraj	if (no < 0)
636208538Sraj		return;
637208538Sraj
638208538Sraj	prop = fdt_get_property(fdtp, no, "stdout", &len);
639208538Sraj
640208538Sraj	/* If /chosen/stdout does not extist, create it */
641208538Sraj	if (prop == NULL || (prop != NULL && len == 0)) {
642208538Sraj
643208538Sraj		bzero(tmp, 10 * sizeof(char));
644208538Sraj		strcpy((char *)&tmp, "serial");
645208538Sraj		if (strlen(ptr) > 3)
646208538Sraj			/* Serial number too long */
647208538Sraj			return;
648208538Sraj
649208538Sraj		strncpy((char *)tmp + 6, ptr, 3);
650208538Sraj		sero = fdt_path_offset(fdtp, (const char *)tmp);
651208538Sraj		if (sero < 0)
652208538Sraj			/*
653208538Sraj			 * If serial device we're trying to assign
654208538Sraj			 * stdout to doesn't exist in DT -- return.
655208538Sraj			 */
656208538Sraj			return;
657208538Sraj
658208538Sraj		fdt_setprop(fdtp, no, "stdout", &tmp,
659208538Sraj		    strlen((char *)&tmp) + 1);
660208538Sraj		fdt_setprop(fdtp, no, "stdin", &tmp,
661208538Sraj		    strlen((char *)&tmp) + 1);
662208538Sraj	}
663208538Sraj}
664208538Sraj
665233230Sraj/*
666233230Sraj * Locate the blob, fix it up and return its location.
667233230Sraj */
668247250Skientzlestatic int
669208538Srajfdt_fixup(void)
670208538Sraj{
671208538Sraj	const char *env;
672208538Sraj	char *ethstr;
673265065Sian	int chosen, eth_no, len;
674208538Sraj	struct sys_info *si;
675208538Sraj
676208538Sraj	env = NULL;
677208538Sraj	eth_no = 0;
678208538Sraj	ethstr = NULL;
679208538Sraj	len = 0;
680208538Sraj
681265065Sian	if (fdtp == NULL && fdt_setup_fdtp() != 0)
682265065Sian		return (0);
683208538Sraj
684208538Sraj	/* Create /chosen node (if not exists) */
685208538Sraj	if ((chosen = fdt_subnode_offset(fdtp, 0, "chosen")) ==
686208538Sraj	    -FDT_ERR_NOTFOUND)
687208538Sraj		chosen = fdt_add_subnode(fdtp, 0, "chosen");
688208538Sraj
689208538Sraj	/* Value assigned to fixup-applied does not matter. */
690208538Sraj	if (fdt_getprop(fdtp, chosen, "fixup-applied", NULL))
691247250Skientzle		return (1);
692208538Sraj
693208538Sraj	/* Acquire sys_info */
694208538Sraj	si = ub_get_sys_info();
695208538Sraj
696208538Sraj	while ((env = ub_env_enum(env)) != NULL) {
697208538Sraj		if (strncmp(env, "eth", 3) == 0 &&
698208538Sraj		    strncmp(env + (strlen(env) - 4), "addr", 4) == 0) {
699208538Sraj			/*
700208538Sraj			 * Handle Ethernet addrs: parse uboot env eth%daddr
701208538Sraj			 */
702208538Sraj
703208538Sraj			if (!eth_no) {
704208538Sraj				/*
705208538Sraj				 * Check how many chars we will need to store
706208538Sraj				 * maximal eth iface number.
707208538Sraj				 */
708208538Sraj				len = strlen(STRINGIFY(TMP_MAX_ETH)) +
709208538Sraj				    strlen("ethernet");
710208538Sraj
711208538Sraj				/*
712208538Sraj				 * Reserve mem for string "ethernet" and len
713208538Sraj				 * chars for iface no.
714208538Sraj				 */
715208538Sraj				ethstr = (char *)malloc(len * sizeof(char));
716208538Sraj				bzero(ethstr, len * sizeof(char));
717208538Sraj				strcpy(ethstr, "ethernet0");
718208538Sraj			}
719208538Sraj
720208538Sraj			/* Modify blob */
721208538Sraj			fixup_ethernet(env, ethstr, &eth_no, len);
722208538Sraj
723208538Sraj		} else if (strcmp(env, "consoledev") == 0)
724208538Sraj			fixup_stdout(env);
725208538Sraj	}
726208538Sraj
727208538Sraj	/* Modify cpu(s) and bus clock frequenties in /cpus node [Hz] */
728208538Sraj	fixup_cpubusfreqs(si->clk_cpu, si->clk_bus);
729208538Sraj
730208538Sraj	/* Fixup memory regions */
731208538Sraj	fixup_memory(si);
732208538Sraj
733208538Sraj	fdt_setprop(fdtp, chosen, "fixup-applied", NULL, 0);
734247250Skientzle	return (1);
735208538Sraj}
736208538Sraj
737243693Sgonzo/*
738247250Skientzle * Copy DTB blob to specified location and return size
739243693Sgonzo */
740208538Srajint
741243693Sgonzofdt_copy(vm_offset_t va)
742243693Sgonzo{
743243693Sgonzo	int err;
744243693Sgonzo
745243693Sgonzo	if (fdtp == NULL) {
746243693Sgonzo		err = fdt_setup_fdtp();
747243693Sgonzo		if (err) {
748265065Sian			printf("No valid device tree blob found!\n");
749243693Sgonzo			return (0);
750243693Sgonzo		}
751243693Sgonzo	}
752243693Sgonzo
753243693Sgonzo	if (fdt_fixup() == 0)
754243693Sgonzo		return (0);
755243693Sgonzo
756247250Skientzle	if (fdtp_va != 0) {
757247250Skientzle		/* Overwrite the FDT with the fixed version. */
758247250Skientzle		/* XXX Is this really appropriate? */
759247250Skientzle		COPYIN(fdtp, fdtp_va, fdtp_size);
760247250Skientzle	}
761243693Sgonzo	COPYIN(fdtp, va, fdtp_size);
762243693Sgonzo	return (fdtp_size);
763243693Sgonzo}
764243693Sgonzo
765243693Sgonzo
766243693Sgonzo
767243693Sgonzoint
768208538Srajcommand_fdt_internal(int argc, char *argv[])
769208538Sraj{
770208538Sraj	cmdf_t *cmdh;
771243693Sgonzo	int flags;
772208538Sraj	char *cmd;
773208538Sraj	int i, err;
774208538Sraj
775208538Sraj	if (argc < 2) {
776208538Sraj		command_errmsg = "usage is 'fdt <command> [<args>]";
777208538Sraj		return (CMD_ERROR);
778208538Sraj	}
779208538Sraj
780208538Sraj	/*
781208538Sraj	 * Validate fdt <command>.
782208538Sraj	 */
783208538Sraj	cmd = strdup(argv[1]);
784208538Sraj	i = 0;
785208538Sraj	cmdh = NULL;
786208538Sraj	while (!(commands[i].name == NULL)) {
787208538Sraj		if (strcmp(cmd, commands[i].name) == 0) {
788208538Sraj			/* found it */
789208538Sraj			cmdh = commands[i].handler;
790243693Sgonzo			flags = commands[i].flags;
791208538Sraj			break;
792208538Sraj		}
793208538Sraj		i++;
794208538Sraj	}
795208538Sraj	if (cmdh == NULL) {
796208538Sraj		command_errmsg = "unknown command";
797208538Sraj		return (CMD_ERROR);
798208538Sraj	}
799208538Sraj
800243693Sgonzo	if (flags & CMD_REQUIRES_BLOB) {
801243693Sgonzo		/*
802243693Sgonzo		 * Check if uboot env vars were parsed already. If not, do it now.
803243693Sgonzo		 */
804243693Sgonzo		if (fdt_fixup() == 0)
805243693Sgonzo			return (CMD_ERROR);
806243693Sgonzo	}
807243693Sgonzo
808208538Sraj	/*
809208538Sraj	 * Call command handler.
810208538Sraj	 */
811208538Sraj	err = (*cmdh)(argc, argv);
812208538Sraj
813208538Sraj	return (err);
814208538Sraj}
815208538Sraj
816208538Srajstatic int
817243693Sgonzofdt_cmd_addr(int argc, char *argv[])
818243693Sgonzo{
819247201Skientzle	struct preloaded_file *fp;
820247045Skientzle	struct fdt_header *hdr;
821247201Skientzle	const char *addr;
822247201Skientzle	char *cp;
823243693Sgonzo
824247201Skientzle	fdt_to_load = NULL;
825247201Skientzle
826243693Sgonzo	if (argc > 2)
827243693Sgonzo		addr = argv[2];
828243693Sgonzo	else {
829243693Sgonzo		sprintf(command_errbuf, "no address specified");
830243693Sgonzo		return (CMD_ERROR);
831243693Sgonzo	}
832243693Sgonzo
833247201Skientzle	hdr = (struct fdt_header *)strtoul(addr, &cp, 16);
834243693Sgonzo	if (cp == addr) {
835243693Sgonzo		sprintf(command_errbuf, "Invalid address: %s", addr);
836243693Sgonzo		return (CMD_ERROR);
837243693Sgonzo	}
838243693Sgonzo
839247201Skientzle	while ((fp = file_findfile(NULL, "dtb")) != NULL) {
840247201Skientzle		file_discard(fp);
841247201Skientzle	}
842243693Sgonzo
843247201Skientzle	fdt_to_load = hdr;
844243693Sgonzo	return (CMD_OK);
845243693Sgonzo}
846243693Sgonzo
847243693Sgonzostatic int
848208538Srajfdt_cmd_cd(int argc, char *argv[])
849208538Sraj{
850208538Sraj	char *path;
851208538Sraj	char tmp[FDT_CWD_LEN];
852208538Sraj	int len, o;
853208538Sraj
854208538Sraj	path = (argc > 2) ? argv[2] : "/";
855208538Sraj
856208538Sraj	if (path[0] == '/') {
857208538Sraj		len = strlen(path);
858208538Sraj		if (len >= FDT_CWD_LEN)
859208538Sraj			goto fail;
860208538Sraj	} else {
861208538Sraj		/* Handle path specification relative to cwd */
862208538Sraj		len = strlen(cwd) + strlen(path) + 1;
863208538Sraj		if (len >= FDT_CWD_LEN)
864208538Sraj			goto fail;
865208538Sraj
866208538Sraj		strcpy(tmp, cwd);
867208538Sraj		strcat(tmp, "/");
868208538Sraj		strcat(tmp, path);
869208538Sraj		path = tmp;
870208538Sraj	}
871208538Sraj
872208538Sraj	o = fdt_path_offset(fdtp, path);
873208538Sraj	if (o < 0) {
874208538Sraj		sprintf(command_errbuf, "could not find node: '%s'", path);
875208538Sraj		return (CMD_ERROR);
876208538Sraj	}
877208538Sraj
878208538Sraj	strcpy(cwd, path);
879208538Sraj	return (CMD_OK);
880208538Sraj
881208538Srajfail:
882208538Sraj	sprintf(command_errbuf, "path too long: %d, max allowed: %d",
883208538Sraj	    len, FDT_CWD_LEN - 1);
884208538Sraj	return (CMD_ERROR);
885208538Sraj}
886208538Sraj
887208538Srajstatic int
888208538Srajfdt_cmd_hdr(int argc __unused, char *argv[] __unused)
889208538Sraj{
890208538Sraj	char line[80];
891208538Sraj	int ver;
892208538Sraj
893208538Sraj	if (fdtp == NULL) {
894208538Sraj		command_errmsg = "no device tree blob pointer?!";
895208538Sraj		return (CMD_ERROR);
896208538Sraj	}
897208538Sraj
898208538Sraj	ver = fdt_version(fdtp);
899208538Sraj	pager_open();
900208538Sraj	sprintf(line, "\nFlattened device tree header (%p):\n", fdtp);
901208538Sraj	pager_output(line);
902208538Sraj	sprintf(line, " magic                   = 0x%08x\n", fdt_magic(fdtp));
903208538Sraj	pager_output(line);
904208538Sraj	sprintf(line, " size                    = %d\n", fdt_totalsize(fdtp));
905208538Sraj	pager_output(line);
906208538Sraj	sprintf(line, " off_dt_struct           = 0x%08x\n",
907208538Sraj	    fdt_off_dt_struct(fdtp));
908208538Sraj	pager_output(line);
909208538Sraj	sprintf(line, " off_dt_strings          = 0x%08x\n",
910208538Sraj	    fdt_off_dt_strings(fdtp));
911208538Sraj	pager_output(line);
912208538Sraj	sprintf(line, " off_mem_rsvmap          = 0x%08x\n",
913208538Sraj	    fdt_off_mem_rsvmap(fdtp));
914208538Sraj	pager_output(line);
915208538Sraj	sprintf(line, " version                 = %d\n", ver);
916208538Sraj	pager_output(line);
917208538Sraj	sprintf(line, " last compatible version = %d\n",
918208538Sraj	    fdt_last_comp_version(fdtp));
919208538Sraj	pager_output(line);
920208538Sraj	if (ver >= 2) {
921208538Sraj		sprintf(line, " boot_cpuid              = %d\n",
922208538Sraj		    fdt_boot_cpuid_phys(fdtp));
923208538Sraj		pager_output(line);
924208538Sraj	}
925208538Sraj	if (ver >= 3) {
926208538Sraj		sprintf(line, " size_dt_strings         = %d\n",
927208538Sraj		    fdt_size_dt_strings(fdtp));
928208538Sraj		pager_output(line);
929208538Sraj	}
930208538Sraj	if (ver >= 17) {
931208538Sraj		sprintf(line, " size_dt_struct          = %d\n",
932208538Sraj		    fdt_size_dt_struct(fdtp));
933208538Sraj		pager_output(line);
934208538Sraj	}
935208538Sraj	pager_close();
936208538Sraj
937208538Sraj	return (CMD_OK);
938208538Sraj}
939208538Sraj
940208538Srajstatic int
941208538Srajfdt_cmd_ls(int argc, char *argv[])
942208538Sraj{
943208538Sraj	const char *prevname[FDT_MAX_DEPTH] = { NULL };
944208538Sraj	const char *name;
945208538Sraj	char *path;
946208538Sraj	int i, o, depth, len;
947208538Sraj
948208538Sraj	path = (argc > 2) ? argv[2] : NULL;
949208538Sraj	if (path == NULL)
950208538Sraj		path = cwd;
951208538Sraj
952208538Sraj	o = fdt_path_offset(fdtp, path);
953208538Sraj	if (o < 0) {
954208538Sraj		sprintf(command_errbuf, "could not find node: '%s'", path);
955208538Sraj		return (CMD_ERROR);
956208538Sraj	}
957208538Sraj
958208538Sraj	for (depth = 0;
959208538Sraj	    (o >= 0) && (depth >= 0);
960208538Sraj	    o = fdt_next_node(fdtp, o, &depth)) {
961208538Sraj
962208538Sraj		name = fdt_get_name(fdtp, o, &len);
963208538Sraj
964208538Sraj		if (depth > FDT_MAX_DEPTH) {
965208538Sraj			printf("max depth exceeded: %d\n", depth);
966208538Sraj			continue;
967208538Sraj		}
968208538Sraj
969208538Sraj		prevname[depth] = name;
970208538Sraj
971208538Sraj		/* Skip root (i = 1) when printing devices */
972208538Sraj		for (i = 1; i <= depth; i++) {
973208538Sraj			if (prevname[i] == NULL)
974208538Sraj				break;
975208538Sraj
976208538Sraj			if (strcmp(cwd, "/") == 0)
977208538Sraj				printf("/");
978208538Sraj			printf("%s", prevname[i]);
979208538Sraj		}
980208538Sraj		printf("\n");
981208538Sraj	}
982208538Sraj
983208538Sraj	return (CMD_OK);
984208538Sraj}
985208538Sraj
986208538Srajstatic __inline int
987208538Srajisprint(int c)
988208538Sraj{
989208538Sraj
990208538Sraj	return (c >= ' ' && c <= 0x7e);
991208538Sraj}
992208538Sraj
993208538Srajstatic int
994208538Srajfdt_isprint(const void *data, int len, int *count)
995208538Sraj{
996208538Sraj	const char *d;
997208538Sraj	char ch;
998208538Sraj	int yesno, i;
999208538Sraj
1000208538Sraj	if (len == 0)
1001208538Sraj		return (0);
1002208538Sraj
1003208538Sraj	d = (const char *)data;
1004208538Sraj	if (d[len - 1] != '\0')
1005208538Sraj		return (0);
1006208538Sraj
1007208538Sraj	*count = 0;
1008208538Sraj	yesno = 1;
1009208538Sraj	for (i = 0; i < len; i++) {
1010208538Sraj		ch = *(d + i);
1011208538Sraj		if (isprint(ch) || (ch == '\0' && i > 0)) {
1012208538Sraj			/* Count strings */
1013208538Sraj			if (ch == '\0')
1014208538Sraj				(*count)++;
1015208538Sraj			continue;
1016208538Sraj		}
1017208538Sraj
1018208538Sraj		yesno = 0;
1019208538Sraj		break;
1020208538Sraj	}
1021208538Sraj
1022208538Sraj	return (yesno);
1023208538Sraj}
1024208538Sraj
1025208538Srajstatic int
1026208538Srajfdt_data_str(const void *data, int len, int count, char **buf)
1027208538Sraj{
1028233323Sraj	char *b, *tmp;
1029208538Sraj	const char *d;
1030233323Sraj	int buf_len, i, l;
1031208538Sraj
1032208538Sraj	/*
1033208538Sraj	 * Calculate the length for the string and allocate memory.
1034208538Sraj	 *
1035233323Sraj	 * Note that 'len' already includes at least one terminator.
1036208538Sraj	 */
1037233323Sraj	buf_len = len;
1038208538Sraj	if (count > 1) {
1039208538Sraj		/*
1040208538Sraj		 * Each token had already a terminator buried in 'len', but we
1041208538Sraj		 * only need one eventually, don't count space for these.
1042208538Sraj		 */
1043233323Sraj		buf_len -= count - 1;
1044208538Sraj
1045208538Sraj		/* Each consecutive token requires a ", " separator. */
1046233323Sraj		buf_len += count * 2;
1047208538Sraj	}
1048208538Sraj
1049233323Sraj	/* Add some space for surrounding double quotes. */
1050233323Sraj	buf_len += count * 2;
1051233323Sraj
1052233323Sraj	/* Note that string being put in 'tmp' may be as big as 'buf_len'. */
1053233323Sraj	b = (char *)malloc(buf_len);
1054233323Sraj	tmp = (char *)malloc(buf_len);
1055208538Sraj	if (b == NULL)
1056233323Sraj		goto error;
1057233323Sraj
1058233323Sraj	if (tmp == NULL) {
1059233323Sraj		free(b);
1060233323Sraj		goto error;
1061233323Sraj	}
1062233323Sraj
1063208538Sraj	b[0] = '\0';
1064208538Sraj
1065208538Sraj	/*
1066208538Sraj	 * Now that we have space, format the string.
1067208538Sraj	 */
1068208538Sraj	i = 0;
1069208538Sraj	do {
1070208538Sraj		d = (const char *)data + i;
1071208538Sraj		l = strlen(d) + 1;
1072208538Sraj
1073208538Sraj		sprintf(tmp, "\"%s\"%s", d,
1074208538Sraj		    (i + l) < len ?  ", " : "");
1075208538Sraj		strcat(b, tmp);
1076208538Sraj
1077208538Sraj		i += l;
1078208538Sraj
1079208538Sraj	} while (i < len);
1080208538Sraj	*buf = b;
1081208538Sraj
1082233323Sraj	free(tmp);
1083233323Sraj
1084208538Sraj	return (0);
1085233323Srajerror:
1086233323Sraj	return (1);
1087208538Sraj}
1088208538Sraj
1089208538Srajstatic int
1090208538Srajfdt_data_cell(const void *data, int len, char **buf)
1091208538Sraj{
1092233323Sraj	char *b, *tmp;
1093208538Sraj	const uint32_t *c;
1094208538Sraj	int count, i, l;
1095208538Sraj
1096208538Sraj	/* Number of cells */
1097208538Sraj	count = len / 4;
1098208538Sraj
1099208538Sraj	/*
1100208538Sraj	 * Calculate the length for the string and allocate memory.
1101208538Sraj	 */
1102208538Sraj
1103208538Sraj	/* Each byte translates to 2 output characters */
1104208538Sraj	l = len * 2;
1105208538Sraj	if (count > 1) {
1106208538Sraj		/* Each consecutive cell requires a " " separator. */
1107208538Sraj		l += (count - 1) * 1;
1108208538Sraj	}
1109208538Sraj	/* Each cell will have a "0x" prefix */
1110208538Sraj	l += count * 2;
1111208538Sraj	/* Space for surrounding <> and terminator */
1112208538Sraj	l += 3;
1113208538Sraj
1114208538Sraj	b = (char *)malloc(l);
1115233323Sraj	tmp = (char *)malloc(l);
1116208538Sraj	if (b == NULL)
1117233323Sraj		goto error;
1118208538Sraj
1119233323Sraj	if (tmp == NULL) {
1120233323Sraj		free(b);
1121233323Sraj		goto error;
1122233323Sraj	}
1123233323Sraj
1124208538Sraj	b[0] = '\0';
1125208538Sraj	strcat(b, "<");
1126208538Sraj
1127208538Sraj	for (i = 0; i < len; i += 4) {
1128208538Sraj		c = (const uint32_t *)((const uint8_t *)data + i);
1129208538Sraj		sprintf(tmp, "0x%08x%s", fdt32_to_cpu(*c),
1130208538Sraj		    i < (len - 4) ? " " : "");
1131208538Sraj		strcat(b, tmp);
1132208538Sraj	}
1133208538Sraj	strcat(b, ">");
1134208538Sraj	*buf = b;
1135208538Sraj
1136233323Sraj	free(tmp);
1137233323Sraj
1138208538Sraj	return (0);
1139233323Srajerror:
1140233323Sraj	return (1);
1141208538Sraj}
1142208538Sraj
1143208538Srajstatic int
1144208538Srajfdt_data_bytes(const void *data, int len, char **buf)
1145208538Sraj{
1146233323Sraj	char *b, *tmp;
1147208538Sraj	const char *d;
1148208538Sraj	int i, l;
1149208538Sraj
1150208538Sraj	/*
1151208538Sraj	 * Calculate the length for the string and allocate memory.
1152208538Sraj	 */
1153208538Sraj
1154208538Sraj	/* Each byte translates to 2 output characters */
1155208538Sraj	l = len * 2;
1156208538Sraj	if (len > 1)
1157208538Sraj		/* Each consecutive byte requires a " " separator. */
1158208538Sraj		l += (len - 1) * 1;
1159208538Sraj	/* Each byte will have a "0x" prefix */
1160208538Sraj	l += len * 2;
1161208538Sraj	/* Space for surrounding [] and terminator. */
1162208538Sraj	l += 3;
1163208538Sraj
1164208538Sraj	b = (char *)malloc(l);
1165233323Sraj	tmp = (char *)malloc(l);
1166208538Sraj	if (b == NULL)
1167233323Sraj		goto error;
1168208538Sraj
1169233323Sraj	if (tmp == NULL) {
1170233323Sraj		free(b);
1171233323Sraj		goto error;
1172233323Sraj	}
1173233323Sraj
1174208538Sraj	b[0] = '\0';
1175208538Sraj	strcat(b, "[");
1176208538Sraj
1177208538Sraj	for (i = 0, d = data; i < len; i++) {
1178208538Sraj		sprintf(tmp, "0x%02x%s", d[i], i < len - 1 ? " " : "");
1179208538Sraj		strcat(b, tmp);
1180208538Sraj	}
1181208538Sraj	strcat(b, "]");
1182208538Sraj	*buf = b;
1183208538Sraj
1184233323Sraj	free(tmp);
1185233323Sraj
1186208538Sraj	return (0);
1187233323Srajerror:
1188233323Sraj	return (1);
1189208538Sraj}
1190208538Sraj
1191208538Srajstatic int
1192208538Srajfdt_data_fmt(const void *data, int len, char **buf)
1193208538Sraj{
1194208538Sraj	int count;
1195208538Sraj
1196208538Sraj	if (len == 0) {
1197208538Sraj		*buf = NULL;
1198208538Sraj		return (1);
1199208538Sraj	}
1200208538Sraj
1201208538Sraj	if (fdt_isprint(data, len, &count))
1202208538Sraj		return (fdt_data_str(data, len, count, buf));
1203208538Sraj
1204208538Sraj	else if ((len % 4) == 0)
1205208538Sraj		return (fdt_data_cell(data, len, buf));
1206208538Sraj
1207208538Sraj	else
1208208538Sraj		return (fdt_data_bytes(data, len, buf));
1209208538Sraj}
1210208538Sraj
1211208538Srajstatic int
1212208538Srajfdt_prop(int offset)
1213208538Sraj{
1214208538Sraj	char *line, *buf;
1215208538Sraj	const struct fdt_property *prop;
1216208538Sraj	const char *name;
1217208538Sraj	const void *data;
1218208538Sraj	int len, rv;
1219208538Sraj
1220208538Sraj	line = NULL;
1221208538Sraj	prop = fdt_offset_ptr(fdtp, offset, sizeof(*prop));
1222208538Sraj	if (prop == NULL)
1223208538Sraj		return (1);
1224208538Sraj
1225208538Sraj	name = fdt_string(fdtp, fdt32_to_cpu(prop->nameoff));
1226208538Sraj	len = fdt32_to_cpu(prop->len);
1227208538Sraj
1228208538Sraj	rv = 0;
1229208538Sraj	buf = NULL;
1230208538Sraj	if (len == 0) {
1231208538Sraj		/* Property without value */
1232208538Sraj		line = (char *)malloc(strlen(name) + 2);
1233208538Sraj		if (line == NULL) {
1234208538Sraj			rv = 2;
1235208538Sraj			goto out2;
1236208538Sraj		}
1237208538Sraj		sprintf(line, "%s\n", name);
1238208538Sraj		goto out1;
1239208538Sraj	}
1240208538Sraj
1241208538Sraj	/*
1242208538Sraj	 * Process property with value
1243208538Sraj	 */
1244208538Sraj	data = prop->data;
1245208538Sraj
1246208538Sraj	if (fdt_data_fmt(data, len, &buf) != 0) {
1247208538Sraj		rv = 3;
1248208538Sraj		goto out2;
1249208538Sraj	}
1250208538Sraj
1251208538Sraj	line = (char *)malloc(strlen(name) + strlen(FDT_PROP_SEP) +
1252208538Sraj	    strlen(buf) + 2);
1253208538Sraj	if (line == NULL) {
1254208538Sraj		sprintf(command_errbuf, "could not allocate space for string");
1255208538Sraj		rv = 4;
1256208538Sraj		goto out2;
1257208538Sraj	}
1258208538Sraj
1259208538Sraj	sprintf(line, "%s" FDT_PROP_SEP "%s\n", name, buf);
1260208538Sraj
1261208538Srajout1:
1262208538Sraj	pager_open();
1263208538Sraj	pager_output(line);
1264208538Sraj	pager_close();
1265208538Sraj
1266208538Srajout2:
1267208538Sraj	if (buf)
1268208538Sraj		free(buf);
1269208538Sraj
1270208538Sraj	if (line)
1271208538Sraj		free(line);
1272208538Sraj
1273208538Sraj	return (rv);
1274208538Sraj}
1275208538Sraj
1276208538Srajstatic int
1277208538Srajfdt_modprop(int nodeoff, char *propname, void *value, char mode)
1278208538Sraj{
1279208538Sraj	uint32_t cells[100];
1280208538Sraj	char *buf;
1281208538Sraj	int len, rv;
1282208538Sraj	const struct fdt_property *p;
1283208538Sraj
1284208538Sraj	p = fdt_get_property(fdtp, nodeoff, propname, NULL);
1285208538Sraj
1286208538Sraj	if (p != NULL) {
1287208538Sraj		if (mode == 1) {
1288208538Sraj			 /* Adding inexistant value in mode 1 is forbidden */
1289208538Sraj			sprintf(command_errbuf, "property already exists!");
1290208538Sraj			return (CMD_ERROR);
1291208538Sraj		}
1292208538Sraj	} else if (mode == 0) {
1293208538Sraj		sprintf(command_errbuf, "property does not exist!");
1294208538Sraj		return (CMD_ERROR);
1295208538Sraj	}
1296208538Sraj	len = strlen(value);
1297208538Sraj	rv = 0;
1298208538Sraj	buf = (char *)value;
1299208538Sraj
1300208538Sraj	switch (*buf) {
1301208538Sraj	case '&':
1302208538Sraj		/* phandles */
1303208538Sraj		break;
1304208538Sraj	case '<':
1305208538Sraj		/* Data cells */
1306208538Sraj		len = fdt_strtovect(buf, (void *)&cells, 100,
1307208538Sraj		    sizeof(uint32_t));
1308208538Sraj
1309208538Sraj		rv = fdt_setprop(fdtp, nodeoff, propname, &cells,
1310208538Sraj		    len * sizeof(uint32_t));
1311208538Sraj		break;
1312208538Sraj	case '[':
1313208538Sraj		/* Data bytes */
1314208538Sraj		len = fdt_strtovect(buf, (void *)&cells, 100,
1315208538Sraj		    sizeof(uint8_t));
1316208538Sraj
1317208538Sraj		rv = fdt_setprop(fdtp, nodeoff, propname, &cells,
1318208538Sraj		    len * sizeof(uint8_t));
1319208538Sraj		break;
1320208538Sraj	case '"':
1321208538Sraj	default:
1322208538Sraj		/* Default -- string */
1323208538Sraj		rv = fdt_setprop_string(fdtp, nodeoff, propname, value);
1324208538Sraj		break;
1325208538Sraj	}
1326208538Sraj
1327233323Sraj	if (rv != 0) {
1328233323Sraj		if (rv == -FDT_ERR_NOSPACE)
1329233323Sraj			sprintf(command_errbuf,
1330233323Sraj			    "Device tree blob is too small!\n");
1331233323Sraj		else
1332233323Sraj			sprintf(command_errbuf,
1333233323Sraj			    "Could not add/modify property!\n");
1334233323Sraj	}
1335208538Sraj	return (rv);
1336208538Sraj}
1337208538Sraj
1338208538Sraj/* Merge strings from argv into a single string */
1339208538Srajstatic int
1340208538Srajfdt_merge_strings(int argc, char *argv[], int start, char **buffer)
1341208538Sraj{
1342208538Sraj	char *buf;
1343208538Sraj	int i, idx, sz;
1344208538Sraj
1345208538Sraj	*buffer = NULL;
1346208538Sraj	sz = 0;
1347208538Sraj
1348208538Sraj	for (i = start; i < argc; i++)
1349208538Sraj		sz += strlen(argv[i]);
1350208538Sraj
1351208538Sraj	/* Additional bytes for whitespaces between args */
1352208538Sraj	sz += argc - start;
1353208538Sraj
1354208538Sraj	buf = (char *)malloc(sizeof(char) * sz);
1355208538Sraj	bzero(buf, sizeof(char) * sz);
1356208538Sraj
1357208538Sraj	if (buf == NULL) {
1358208538Sraj		sprintf(command_errbuf, "could not allocate space "
1359208538Sraj		    "for string");
1360208538Sraj		return (1);
1361208538Sraj	}
1362208538Sraj
1363208538Sraj	idx = 0;
1364208538Sraj	for (i = start, idx = 0; i < argc; i++) {
1365208538Sraj		strcpy(buf + idx, argv[i]);
1366208538Sraj		idx += strlen(argv[i]);
1367208538Sraj		buf[idx] = ' ';
1368208538Sraj		idx++;
1369208538Sraj	}
1370208538Sraj	buf[sz - 1] = '\0';
1371208538Sraj	*buffer = buf;
1372208538Sraj	return (0);
1373208538Sraj}
1374208538Sraj
1375208538Sraj/* Extract offset and name of node/property from a given path */
1376208538Srajstatic int
1377208538Srajfdt_extract_nameloc(char **pathp, char **namep, int *nodeoff)
1378208538Sraj{
1379208538Sraj	int o;
1380208538Sraj	char *path = *pathp, *name = NULL, *subpath = NULL;
1381208538Sraj
1382208538Sraj	subpath = strrchr(path, '/');
1383208538Sraj	if (subpath == NULL) {
1384208538Sraj		o = fdt_path_offset(fdtp, cwd);
1385208538Sraj		name = path;
1386208538Sraj		path = (char *)&cwd;
1387208538Sraj	} else {
1388208538Sraj		*subpath = '\0';
1389208538Sraj		if (strlen(path) == 0)
1390208538Sraj			path = cwd;
1391208538Sraj
1392208538Sraj		name = subpath + 1;
1393208538Sraj		o = fdt_path_offset(fdtp, path);
1394208538Sraj	}
1395208538Sraj
1396208538Sraj	if (strlen(name) == 0) {
1397208538Sraj		sprintf(command_errbuf, "name not specified");
1398208538Sraj		return (1);
1399208538Sraj	}
1400208538Sraj	if (o < 0) {
1401208538Sraj		sprintf(command_errbuf, "could not find node: '%s'", path);
1402208538Sraj		return (1);
1403208538Sraj	}
1404208538Sraj	*namep = name;
1405208538Sraj	*nodeoff = o;
1406208538Sraj	*pathp = path;
1407208538Sraj	return (0);
1408208538Sraj}
1409208538Sraj
1410208538Srajstatic int
1411208538Srajfdt_cmd_prop(int argc, char *argv[])
1412208538Sraj{
1413208538Sraj	char *path, *propname, *value;
1414208538Sraj	int o, next, depth, rv;
1415208538Sraj	uint32_t tag;
1416208538Sraj
1417208538Sraj	path = (argc > 2) ? argv[2] : NULL;
1418208538Sraj
1419208538Sraj	value = NULL;
1420208538Sraj
1421208538Sraj	if (argc > 3) {
1422208538Sraj		/* Merge property value strings into one */
1423208538Sraj		if (fdt_merge_strings(argc, argv, 3, &value) != 0)
1424208538Sraj			return (CMD_ERROR);
1425208538Sraj	} else
1426208538Sraj		value = NULL;
1427208538Sraj
1428208538Sraj	if (path == NULL)
1429208538Sraj		path = cwd;
1430208538Sraj
1431208538Sraj	rv = CMD_OK;
1432208538Sraj
1433208538Sraj	if (value) {
1434208538Sraj		/* If value is specified -- try to modify prop. */
1435208538Sraj		if (fdt_extract_nameloc(&path, &propname, &o) != 0)
1436208538Sraj			return (CMD_ERROR);
1437208538Sraj
1438208538Sraj		rv = fdt_modprop(o, propname, value, 0);
1439208538Sraj		if (rv)
1440208538Sraj			return (CMD_ERROR);
1441208538Sraj		return (CMD_OK);
1442208538Sraj
1443208538Sraj	}
1444208538Sraj	/* User wants to display properties */
1445208538Sraj	o = fdt_path_offset(fdtp, path);
1446208538Sraj
1447208538Sraj	if (o < 0) {
1448208538Sraj		sprintf(command_errbuf, "could not find node: '%s'", path);
1449208538Sraj		rv = CMD_ERROR;
1450208538Sraj		goto out;
1451208538Sraj	}
1452208538Sraj
1453208538Sraj	depth = 0;
1454208538Sraj	while (depth >= 0) {
1455208538Sraj		tag = fdt_next_tag(fdtp, o, &next);
1456208538Sraj		switch (tag) {
1457208538Sraj		case FDT_NOP:
1458208538Sraj			break;
1459208538Sraj		case FDT_PROP:
1460208538Sraj			if (depth > 1)
1461208538Sraj				/* Don't process properties of nested nodes */
1462208538Sraj				break;
1463208538Sraj
1464208538Sraj			if (fdt_prop(o) != 0) {
1465208538Sraj				sprintf(command_errbuf, "could not process "
1466208538Sraj				    "property");
1467208538Sraj				rv = CMD_ERROR;
1468208538Sraj				goto out;
1469208538Sraj			}
1470208538Sraj			break;
1471208538Sraj		case FDT_BEGIN_NODE:
1472208538Sraj			depth++;
1473208538Sraj			if (depth > FDT_MAX_DEPTH) {
1474208538Sraj				printf("warning: nesting too deep: %d\n",
1475208538Sraj				    depth);
1476208538Sraj				goto out;
1477208538Sraj			}
1478208538Sraj			break;
1479208538Sraj		case FDT_END_NODE:
1480208538Sraj			depth--;
1481208538Sraj			if (depth == 0)
1482208538Sraj				/*
1483208538Sraj				 * This is the end of our starting node, force
1484208538Sraj				 * the loop finish.
1485208538Sraj				 */
1486208538Sraj				depth--;
1487208538Sraj			break;
1488208538Sraj		}
1489208538Sraj		o = next;
1490208538Sraj	}
1491208538Srajout:
1492208538Sraj	return (rv);
1493208538Sraj}
1494208538Sraj
1495208538Srajstatic int
1496208538Srajfdt_cmd_mkprop(int argc, char *argv[])
1497208538Sraj{
1498208538Sraj	int o;
1499208538Sraj	char *path, *propname, *value;
1500208538Sraj
1501208538Sraj	path = (argc > 2) ? argv[2] : NULL;
1502208538Sraj
1503208538Sraj	value = NULL;
1504208538Sraj
1505208538Sraj	if (argc > 3) {
1506208538Sraj		/* Merge property value strings into one */
1507208538Sraj		if (fdt_merge_strings(argc, argv, 3, &value) != 0)
1508208538Sraj			return (CMD_ERROR);
1509208538Sraj	} else
1510208538Sraj		value = NULL;
1511208538Sraj
1512208538Sraj	if (fdt_extract_nameloc(&path, &propname, &o) != 0)
1513208538Sraj		return (CMD_ERROR);
1514208538Sraj
1515208538Sraj	if (fdt_modprop(o, propname, value, 1))
1516208538Sraj		return (CMD_ERROR);
1517208538Sraj
1518208538Sraj	return (CMD_OK);
1519208538Sraj}
1520208538Sraj
1521208538Srajstatic int
1522208538Srajfdt_cmd_rm(int argc, char *argv[])
1523208538Sraj{
1524208538Sraj	int o, rv;
1525208538Sraj	char *path = NULL, *propname;
1526208538Sraj
1527208538Sraj	if (argc > 2)
1528208538Sraj		path = argv[2];
1529208538Sraj	else {
1530208538Sraj		sprintf(command_errbuf, "no node/property name specified");
1531208538Sraj		return (CMD_ERROR);
1532208538Sraj	}
1533208538Sraj
1534208538Sraj	o = fdt_path_offset(fdtp, path);
1535208538Sraj	if (o < 0) {
1536208538Sraj		/* If node not found -- try to find & delete property */
1537208538Sraj		if (fdt_extract_nameloc(&path, &propname, &o) != 0)
1538208538Sraj			return (CMD_ERROR);
1539208538Sraj
1540208538Sraj		if ((rv = fdt_delprop(fdtp, o, propname)) != 0) {
1541208538Sraj			sprintf(command_errbuf, "could not delete"
1542208538Sraj			    "%s\n", (rv == -FDT_ERR_NOTFOUND) ?
1543208538Sraj			    "(property/node does not exist)" : "");
1544208538Sraj			return (CMD_ERROR);
1545208538Sraj
1546208538Sraj		} else
1547208538Sraj			return (CMD_OK);
1548208538Sraj	}
1549208538Sraj	/* If node exists -- remove node */
1550208538Sraj	rv = fdt_del_node(fdtp, o);
1551208538Sraj	if (rv) {
1552208538Sraj		sprintf(command_errbuf, "could not delete node");
1553208538Sraj		return (CMD_ERROR);
1554208538Sraj	}
1555208538Sraj	return (CMD_OK);
1556208538Sraj}
1557208538Sraj
1558208538Srajstatic int
1559208538Srajfdt_cmd_mknode(int argc, char *argv[])
1560208538Sraj{
1561208538Sraj	int o, rv;
1562208538Sraj	char *path = NULL, *nodename = NULL;
1563208538Sraj
1564208538Sraj	if (argc > 2)
1565208538Sraj		path = argv[2];
1566208538Sraj	else {
1567208538Sraj		sprintf(command_errbuf, "no node name specified");
1568208538Sraj		return (CMD_ERROR);
1569208538Sraj	}
1570208538Sraj
1571208538Sraj	if (fdt_extract_nameloc(&path, &nodename, &o) != 0)
1572208538Sraj		return (CMD_ERROR);
1573208538Sraj
1574208538Sraj	rv = fdt_add_subnode(fdtp, o, nodename);
1575208538Sraj
1576208538Sraj	if (rv < 0) {
1577233323Sraj		if (rv == -FDT_ERR_NOSPACE)
1578233323Sraj			sprintf(command_errbuf,
1579233323Sraj			    "Device tree blob is too small!\n");
1580233323Sraj		else
1581233323Sraj			sprintf(command_errbuf,
1582233323Sraj			    "Could not add node!\n");
1583208538Sraj		return (CMD_ERROR);
1584208538Sraj	}
1585208538Sraj	return (CMD_OK);
1586208538Sraj}
1587208538Sraj
1588208538Srajstatic int
1589208538Srajfdt_cmd_pwd(int argc, char *argv[])
1590208538Sraj{
1591233323Sraj	char line[FDT_CWD_LEN];
1592208538Sraj
1593208538Sraj	pager_open();
1594208538Sraj	sprintf(line, "%s\n", cwd);
1595208538Sraj	pager_output(line);
1596208538Sraj	pager_close();
1597208538Sraj	return (CMD_OK);
1598208538Sraj}
1599208538Sraj
1600208538Srajstatic int
1601243693Sgonzofdt_cmd_mres(int argc, char *argv[])
1602243693Sgonzo{
1603243693Sgonzo	uint64_t start, size;
1604243693Sgonzo	int i, total;
1605243693Sgonzo	char line[80];
1606243693Sgonzo
1607243693Sgonzo	pager_open();
1608243693Sgonzo	total = fdt_num_mem_rsv(fdtp);
1609243693Sgonzo	if (total > 0) {
1610243693Sgonzo		pager_output("Reserved memory regions:\n");
1611243693Sgonzo		for (i = 0; i < total; i++) {
1612243693Sgonzo			fdt_get_mem_rsv(fdtp, i, &start, &size);
1613243693Sgonzo			sprintf(line, "reg#%d: (start: 0x%jx, size: 0x%jx)\n",
1614243693Sgonzo			    i, start, size);
1615243693Sgonzo			pager_output(line);
1616243693Sgonzo		}
1617243693Sgonzo	} else
1618243693Sgonzo		pager_output("No reserved memory regions\n");
1619243693Sgonzo	pager_close();
1620243693Sgonzo
1621243693Sgonzo	return (CMD_OK);
1622243693Sgonzo}
1623243693Sgonzo
1624243693Sgonzostatic int
1625208538Srajfdt_cmd_nyi(int argc, char *argv[])
1626208538Sraj{
1627208538Sraj
1628208538Sraj	printf("command not yet implemented\n");
1629208538Sraj	return (CMD_ERROR);
1630208538Sraj}
1631