Fixed tools/env utilities
[u-boot-openmoko/mini2440.git] / common / cmd_dynenv.c
blob91f7623605efb3ba5a5b26e897e8dcaa1eb28f5f
1 /*
2 * (C) Copyright 2006-2007 OpenMoko, Inc.
3 * Author: Harald Welte <laforge@openmoko.org>
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public License as
7 * published by the Free Software Foundation; either version 2 of
8 * the License, or (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
18 * MA 02111-1307 USA
21 #include <common.h>
22 #include <command.h>
23 #include <malloc.h>
24 #include <environment.h>
25 #include <nand.h>
26 #include <util.h>
27 #include <asm/errno.h>
29 #if defined(CFG_ENV_OFFSET_OOB)
31 int do_dynenv(cmd_tbl_t *cmdtp, int flag, int argc, char *argv[])
33 struct mtd_info *mtd = &nand_info[0];
34 int ret, size = 8;
35 uint8_t *buf;
37 char *cmd = argv[1];
39 buf = malloc(mtd->oobsize);
40 if (!buf)
41 return -ENOMEM;
43 ret = mtd->read_oob(mtd, 8, size, (size_t *) &size, (u_char *) buf);
44 if (!strcmp(cmd, "get")) {
46 if (buf[0] == 'E' && buf[1] == 'N' &&
47 buf[2] == 'V' && buf[3] == '0')
48 printf("0x%08x\n", *((u_int32_t *) &buf[4]));
49 else
50 printf("No dynamic environment marker in OOB block 0\n");
52 } else if (!strcmp(cmd, "set")) {
53 unsigned long addr, dummy;
55 if (argc < 3)
56 goto usage;
58 buf[0] = 'E';
59 buf[1] = 'N';
60 buf[2] = 'V';
61 buf[3] = '0';
63 if (arg_off_size(argc-2, argv+2, mtd, &addr, &dummy, 1) < 0) {
64 printf("Offset or partition name expected\n");
65 goto fail;
67 if (!ret) {
68 uint8_t tmp[4];
69 int i;
71 memcpy(&tmp, &addr, 4);
72 for (i = 0; i != 4; i++)
73 if (tmp[i] & ~buf[i+4]) {
74 printf("ERROR: erase OOB block to "
75 "write this value\n");
76 goto fail;
79 memcpy(buf+4, &addr, 4);
81 printf("%02x %02x %02x %02x - %02x %02x %02x %02x\n",
82 buf[0], buf[1], buf[2], buf[3],
83 buf[4], buf[5], buf[6], buf[7]);
85 ret = mtd->write_oob(mtd, 8, size, (size_t *) &size, (u_char *) buf);
86 if (!ret)
87 CFG_ENV_OFFSET = addr;
88 } else
89 goto usage;
91 free(buf);
92 return ret;
94 usage:
95 printf("Usage:\n%s\n", cmdtp->usage);
96 fail:
97 free(buf);
98 return 1;
101 U_BOOT_CMD(dynenv, 3, 1, do_dynenv,
102 "dynenv - dynamically placed (NAND) environment\n",
103 "dynenv set off - set enviromnent offset\n"
104 "dynenv get - get environment offset\n");
106 #endif /* CFG_ENV_OFFSET_OOB */