recovery: update is modified to support various image
[kernel/u-boot.git] / recovery / board / samsung / universal / universal.c
index f84e926..02a4388 100644 (file)
@@ -1,39 +1,28 @@
 /*
- * (C) Copyright 2010 Samsung Electronics
- *
- * See file CREDITS for list of people who contributed to this
- * project.
- *
- * This program is free software; you can redistribute it and/or
- * modify it under the terms of the GNU General Public License as
- * published by the Free Software Foundation; either version 2 of
- * the License, or (at your option) any later version.
- *
- * This program is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
- * GNU General Public License for more details.
- *
- * You should have received a copy of the GNU General Public License
- * along with this program; if not, write to the Free Software
- * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
- * MA 02111-1307 USA
+ * Copyright (C) 2009-2010 Samsung Electronics
  */
 
 #include <common.h>
-#include <linux/mtd/compat.h>
 #include <linux/mtd/mtd.h>
 #include <linux/mtd/onenand.h>
 #include <asm/io.h>
 #include <asm/arch/gpio.h>
 #include <asm/arch/keypad.h>
-#include "../../../onenand.h"
+#include "recovery.h"
+#include "onenand.h"
+
+#ifdef RECOVERY_DEBUG
+#define        PUTS(s) serial_puts(DEBUG_MARK""s)
+#else
+#define PUTS(s)
+#endif
 
 typedef int (init_fnc_t) (void);
 
 DECLARE_GLOBAL_DATA_PTR;
 
-static struct s5pc110_gpio *s5pc110_gpio;
+static struct s5pc110_gpio *gpio_base =
+       (struct s5pc110_gpio *) S5PC110_GPIO_BASE;
 
 static void sdelay(unsigned long usec)
 {
@@ -67,13 +56,13 @@ static int check_keypad(void)
 
        for (i = 0; i < row_num; i++) {
                /* Set GPH3[3:0] to KP_ROW[3:0] */
-               gpio_cfg_pin(&s5pc110_gpio->gpio_h3, i, 0x3);
-               gpio_set_pull(&s5pc110_gpio->gpio_h3, i, GPIO_PULL_UP);
+               gpio_cfg_pin(&gpio_base->gpio_h3, i, 0x3);
+               gpio_set_pull(&gpio_base->gpio_h3, i, GPIO_PULL_UP);
        }
 
        for (i = 0; i < col_num; i++)
                /* Set GPH2[3:0] to KP_COL[3:0] */
-               gpio_cfg_pin(&s5pc110_gpio->gpio_h2, i, 0x3);
+               gpio_cfg_pin(&gpio_base->gpio_h2, i, 0x3);
 
        reg = S5PC110_KEYPAD_BASE;
        col_mask = S5PC110_KEYIFCOLEN_MASK;
@@ -110,68 +99,139 @@ static int check_block(void)
        struct mtd_info *mtd = onenand_ops->mtd;
        struct onenand_chip *this = mtd->priv;
        int i;
-       int page_to_check = 4;
-       int ret, retlen = 0;
-       ulong blocksize = 1 << this->erase_shift;
-       ulong pagesize = 1 << this->page_shift;
-       u_char *down_ram_addr;
-       ulong uboot_addr;
-       u_char verify_buf[0x10];
-
-       down_ram_addr = (unsigned char *)CONFIG_SYS_DOWN_ADDR;
-       uboot_addr = CONFIG_RECOVERY_UBOOT_BLOCK << this->erase_shift;
-
-       onenand_ops->read(uboot_addr, blocksize, &retlen, down_ram_addr, 0);
-       if (retlen != blocksize)
+       int retlen = 0;
+       u32 from = CONFIG_RECOVERY_UBOOT_BLOCK << this->erase_shift;
+       u32 len = 1 << this->erase_shift;
+       u32 *buf = (u32 *)CONFIG_SYS_DOWN_ADDR;
+
+       /* check first page of bootloader*/
+       onenand_ops->read(from, len, (ssize_t *)&retlen, (u_char *)buf, 0);
+       if (retlen != len)
                return 1;
 
-       memset(verify_buf, 0xFF, sizeof(verify_buf));
+       for (i = 0; i < (this->writesize / sizeof(this->writesize)); i++)
+               if (*(buf + i) != 0xffffffff)
+                       return 0;
 
-       for (i = 0; i < page_to_check; i++) {
-               ret = memcmp(down_ram_addr + pagesize*i, verify_buf,
-                               sizeof(verify_buf));
-               if (ret)
-                       break;
-       }
+       return 1;
+}
 
-       if (i == page_to_check)
+int board_check_condition(void)
+{
+       if (check_keypad()) {
+               PUTS("check: manual\n");
                return 1;
+       }
+
+       if (check_block()) {
+               PUTS("check: bootloader broken\n");
+               return 2;
+       }
 
        return 0;
 }
 
-int board_check_condition(void)
+int board_load_bootloader(unsigned char *buf)
 {
-       if (check_keypad())
-               return 1;
+       struct mtd_info *mtd = &onenand_mtd;
+       struct onenand_chip *this = mtd->priv;
+       u32 ofs, len, retlen;
 
-       if (check_block())
-               return 1;
+       ofs = CONFIG_RECOVERY_UBOOT_BLOCK << this->erase_shift;
+       len = CONFIG_SYS_MONITOR_LEN;
+
+       mtd->read(mtd, ofs, len, &retlen, buf);
+
+       if (len != retlen)
+               return -1;
 
        return 0;
 }
 
-int board_load_uboot(unsigned char *buf)
+int board_lock_recoveryblock(void)
 {
        struct mtd_info *mtd = &onenand_mtd;
        struct onenand_chip *this = mtd->priv;
-       int offset;
-       size_t size;
-       size_t ret;
+       u32 ofs = 0;
+       u32 len = CONFIG_RECOVERY_UBOOT_BLOCK << this->erase_shift;
+       int ret = 0;
 
-       offset = CONFIG_RECOVERY_UBOOT_BLOCK << this->erase_shift;
-       size = CONFIG_SYS_MONITOR_LEN;
+       /* lock-tight the recovery block */
+       if (this->lock_tight != NULL)
+               ret = this->lock_tight(mtd, ofs, len);
 
-       mtd->read(mtd, offset, size, &ret, buf);
+       return ret;
+}
 
-       if (size != ret)
-               return -1;
+void board_update_init(void)
+{
+       struct mtd_info *mtd = &onenand_mtd;
+       struct onenand_chip *this = mtd->priv;
+
+       /* Unlock whole block */
+       this->unlock_all(mtd);
+}
+
+int board_update_image(u32 *buf, u32 len)
+{
+       struct onenand_op *onenand_ops = onenand_get_interface();
+       struct mtd_info *mtd = &onenand_mtd;
+       struct onenand_chip *this = mtd->priv;
+       u32 ofs;
+       u32 ipl_addr = 0;
+       u32 ipl_edge = CONFIG_ONENAND_START_PAGE << this->page_shift;
+       u32 recovery_addr = ipl_edge;
+       u32 recovery_edge = CONFIG_RECOVERY_UBOOT_BLOCK << this->erase_shift;
+       u32 bootloader_addr = recovery_edge;
+       u32 bootloader_edge = CONFIG_RECOVERY_BOOT_BLOCKS << this->erase_shift;
+       int ret, retlen;
+
+       if (len > bootloader_addr) {
+               if (*(buf + bootloader_addr/sizeof(buf)) == 0xea000012) {
+                       /* case: IPL + Recovery + bootloader */
+                       PUTS("target: ipl + recovery + bootloader\n");
+                       ofs = ipl_addr;
+                       /* len = bootloader_edge; */
+               } else {
+                       /* case: unknown format */
+                       PUTS("target: unknown\n");
+                       return 1;
+               }
+       } else {
+               if (*(buf + recovery_addr/sizeof(buf)) == 0xea000012 &&
+                       *(buf + recovery_addr/sizeof(buf) - 1) == 0x00000000) {
+                       /* case: old image (IPL + bootloader) */
+                       PUTS("target: ipl + bootloader (old type)\n");
+                       ofs = ipl_addr;
+                       /* len = recovery_edge; */
+               } else {
+                       /* case: bootloader only */
+                       PUTS("target: bootloader\n");
+                       ofs = bootloader_addr;
+                       /* len = bootloader_edge - recovery_edge; */
+               }
+       }
+
+       /* Erase */
+       ret = onenand_ops->erase(ofs, len, 0);
+       if (ret)
+               return ret;
+
+       /* Write */
+       onenand_ops->write(ofs, len, (ssize_t *)&retlen, (u_char *)buf);
+       if (ret)
+               return ret;
 
        return 0;
 }
 
 void board_recovery_init(void)
 {
-       /* Set Initial global variables */
-       s5pc110_gpio = (struct s5pc110_gpio *) S5PC110_GPIO_BASE;
+       /* set GPIO to enable UART2 */
+       gpio_cfg_pin(&gpio_base->gpio_a1, 0, 0x2);
+       gpio_cfg_pin(&gpio_base->gpio_a1, 1, 0x2);
+
+       /* UART_SEL MP0_5[7] at S5PC110 */
+       gpio_direction_output(&gpio_base->gpio_mp0_5, 7, 0x1);
+       gpio_set_pull(&gpio_base->gpio_mp0_5, 7, GPIO_PULL_DOWN);
 }