env_nand_oob.patch
[u-boot-openmoko/mini2440.git] / common / cmd_dynenv.c
blobcd7f662b743e9dca977aacf775beb5df72f9963a
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 <asm/errno.h>
28 #if defined(CFG_ENV_OFFSET_OOB)
30 int do_dynenv(cmd_tbl_t *cmdtp, int flag, int argc, char *argv[])
32 struct mtd_info *mtd = &nand_info[0];
33 int ret, size = 8;
34 uint8_t *buf;
36 char *cmd = argv[1];
38 buf = malloc(mtd->oobsize);
39 if (!buf)
40 return -ENOMEM;
42 if (!strcmp(cmd, "get")) {
43 ret = mtd->read_oob(mtd, 8, size, (size_t *) &size, (u_char *) buf);
45 if (buf[0] == 'E' && buf[1] == 'N' &&
46 buf[2] == 'V' && buf[3] == '0')
47 printf("0x%08x\n", *((u_int32_t *) &buf[4]));
48 else
49 printf("No dynamic environment marker in OOB block 0\n");
51 } else if (!strcmp(cmd, "set")) {
52 unsigned long addr;
53 if (argc < 3)
54 goto usage;
56 buf[0] = 'E';
57 buf[1] = 'N';
58 buf[2] = 'V';
59 buf[3] = '0';
60 addr = simple_strtoul(argv[2], NULL, 16);
61 memcpy(buf+4, &addr, 4);
63 printf("%02x %02x %02x %02x - %02x %02x %02x %02x\n",
64 buf[0], buf[1], buf[2], buf[3],
65 buf[4], buf[5], buf[6], buf[7]);
67 ret = mtd->write_oob(mtd, 8, size, (size_t *) &size, (u_char *) buf);
68 } else
69 goto usage;
71 free(buf);
72 return ret;
74 usage:
75 free(buf);
76 printf("Usage:\n%s\n", cmdtp->usage);
77 return 1;
80 U_BOOT_CMD(dynenv, 3, 1, do_dynenv,
81 "dynenv - dynamically placed (NAND) environment\n",
82 "dynenv set off - set enviromnent offset\n"
83 "dynenv get - get environment offset\n");
85 #endif /* CFG_ENV_OFFSET_OOB */