env_nand.c 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. /*
  2. * (C) Copyright 2004
  3. * Jian Zhang, Texas Instruments, jzhang@ti.com.
  4. * (C) Copyright 2000-2004
  5. * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
  6. *
  7. * (C) Copyright 2001 Sysgo Real-Time Solutions, GmbH <www.elinos.com>
  8. * Andreas Heppel <aheppel@sysgo.de>
  9. * See file CREDITS for list of people who contributed to this
  10. * project.
  11. *
  12. * This program is free software; you can redistribute it and/or
  13. * modify it under the terms of the GNU General Public License as
  14. * published by the Free Software Foundation; either version 2 of
  15. * the License, or (at your option) any later version.
  16. *
  17. * This program is distributed in the hope that it will be useful,
  18. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  19. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  20. * GNU General Public License for more details.
  21. *
  22. * You should have received a copy of the GNU General Public License
  23. * along with this program; if not, write to the Free Software
  24. * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
  25. * MA 02111-1307 USA
  26. */
  27. /* #define DEBUG */
  28. #include <common.h>
  29. #if defined(CFG_ENV_IS_IN_NAND) /* Environment is in Nand Flash */
  30. #include <command.h>
  31. #include <environment.h>
  32. #include <linux/stddef.h>
  33. #include <malloc.h>
  34. #include <linux/mtd/nand.h>
  35. #if ((CONFIG_COMMANDS&(CFG_CMD_ENV|CFG_CMD_NAND)) == (CFG_CMD_ENV|CFG_CMD_NAND))
  36. #define CMD_SAVEENV
  37. #endif
  38. #if defined(CFG_ENV_SIZE_REDUND)
  39. #error CFG_ENV_SIZE_REDUND not supported yet
  40. #endif
  41. #if defined(CFG_ENV_ADDR_REDUND)
  42. #error CFG_ENV_ADDR_REDUND and CFG_ENV_IS_IN_NAND not supported yet
  43. #endif
  44. #ifdef CONFIG_INFERNO
  45. #error CONFIG_INFERNO not supported yet
  46. #endif
  47. /* references to names in cmd_nand.c */
  48. #define NANDRW_READ 0x01
  49. #define NANDRW_WRITE 0x00
  50. #define NANDRW_JFFS2 0x02
  51. extern struct nand_chip nand_dev_desc[];
  52. int nand_rw (struct nand_chip* nand, int cmd,
  53. size_t start, size_t len,
  54. size_t * retlen, u_char * buf);
  55. int nand_erase(struct nand_chip* nand, size_t ofs,
  56. size_t len, int clean);
  57. /* references to names in env_common.c */
  58. extern uchar default_environment[];
  59. extern int default_environment_size;
  60. char * env_name_spec = "NAND";
  61. #ifdef ENV_IS_EMBEDDED
  62. extern uchar environment[];
  63. env_t *env_ptr = (env_t *)(&environment[0]);
  64. #else /* ! ENV_IS_EMBEDDED */
  65. env_t *env_ptr = 0;
  66. #endif /* ENV_IS_EMBEDDED */
  67. /* local functions */
  68. static void use_default(void);
  69. uchar env_get_char_spec (int index)
  70. {
  71. DECLARE_GLOBAL_DATA_PTR;
  72. return ( *((uchar *)(gd->env_addr + index)) );
  73. }
  74. /* this is called before nand_init()
  75. * so we can't read Nand to validate env data.
  76. * Mark it OK for now. env_relocate() in env_common.c
  77. * will call our relocate function which will does
  78. * the real validation.
  79. */
  80. int env_init(void)
  81. {
  82. DECLARE_GLOBAL_DATA_PTR;
  83. gd->env_addr = (ulong)&default_environment[0];
  84. gd->env_valid = 1;
  85. return (0);
  86. }
  87. #ifdef CMD_SAVEENV
  88. int saveenv(void)
  89. {
  90. int total, ret = 0;
  91. puts ("Erasing Nand...");
  92. if (nand_erase(nand_dev_desc + 0, CFG_ENV_OFFSET, CFG_ENV_SIZE, 0))
  93. return 1;
  94. puts ("Writing to Nand... ");
  95. ret = nand_rw(nand_dev_desc + 0,
  96. NANDRW_WRITE | NANDRW_JFFS2, CFG_ENV_OFFSET, CFG_ENV_SIZE,
  97. &total, (u_char*)env_ptr);
  98. if (ret || total != CFG_ENV_SIZE)
  99. return 1;
  100. puts ("done\n");
  101. return ret;
  102. }
  103. #endif /* CMD_SAVEENV */
  104. void env_relocate_spec (void)
  105. {
  106. #if !defined(ENV_IS_EMBEDDED)
  107. int ret, total;
  108. ret = nand_rw(nand_dev_desc + 0,
  109. NANDRW_READ | NANDRW_JFFS2, CFG_ENV_OFFSET, CFG_ENV_SIZE,
  110. &total, (u_char*)env_ptr);
  111. if (ret || total != CFG_ENV_SIZE)
  112. return use_default();
  113. if (crc32(0, env_ptr->data, ENV_SIZE) != env_ptr->crc)
  114. return use_default();
  115. #endif /* ! ENV_IS_EMBEDDED */
  116. }
  117. static void use_default()
  118. {
  119. DECLARE_GLOBAL_DATA_PTR;
  120. puts ("*** Warning - bad CRC or NAND, using default environment\n\n");
  121. if (default_environment_size > CFG_ENV_SIZE){
  122. puts ("*** Error - default environment is too large\n\n");
  123. return;
  124. }
  125. memset (env_ptr, 0, sizeof(env_t));
  126. memcpy (env_ptr->data,
  127. default_environment,
  128. default_environment_size);
  129. env_ptr->crc = crc32(0, env_ptr->data, ENV_SIZE);
  130. gd->env_valid = 1;
  131. }
  132. #endif /* CFG_ENV_IS_IN_NAND */