Dave Chapman | da8bff1 | 2008-10-12 16:46:01 +0000 | [diff] [blame] | 1 | /*************************************************************************** |
| 2 | * __________ __ ___. |
| 3 | * Open \______ \ ____ ____ | | _\_ |__ _______ ___ |
| 4 | * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ / |
| 5 | * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < < |
| 6 | * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \ |
| 7 | * \/ \/ \/ \/ \/ |
Dave Chapman | 08c41d4 | 2008-10-19 14:11:01 +0000 | [diff] [blame] | 8 | * $Id$ |
Dave Chapman | da8bff1 | 2008-10-12 16:46:01 +0000 | [diff] [blame] | 9 | * |
| 10 | * Copyright (C) 2008 by Rafaël Carré |
| 11 | * |
| 12 | * Based on Rockbox iriver bootloader by Linus Nielsen Feltzing |
| 13 | * and the ipodlinux bootloader by Daniel Palffy and Bernard Leach |
| 14 | * |
| 15 | * This program is free software; you can redistribute it and/or |
| 16 | * modify it under the terms of the GNU General Public License |
| 17 | * as published by the Free Software Foundation; either version 2 |
| 18 | * of the License, or (at your option) any later version. |
| 19 | * |
| 20 | * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY |
| 21 | * KIND, either express or implied. |
| 22 | * |
| 23 | ****************************************************************************/ |
| 24 | |
| 25 | #include <stdio.h> |
| 26 | #include <system.h> |
| 27 | #include <inttypes.h> |
Dave Chapman | da8bff1 | 2008-10-12 16:46:01 +0000 | [diff] [blame] | 28 | #include "config.h" |
Thomas Martitz | 0e2286f | 2010-08-12 13:38:25 +0000 | [diff] [blame] | 29 | #include "gcc_extensions.h" |
Michael Sevakis | 4ea4cdf | 2014-08-08 02:28:11 -0400 | [diff] [blame] | 30 | #include "../kernel-internal.h" |
Dave Chapman | 8df1a7e | 2008-10-29 01:42:03 +0000 | [diff] [blame] | 31 | #include "lcd.h" |
Amaury Pouly | 761e798 | 2015-01-08 22:30:22 +0100 | [diff] [blame] | 32 | #ifdef HAVE_BOOTLOADER_USB_MODE |
Rafaël Carré | b812465 | 2010-06-18 01:47:39 +0000 | [diff] [blame] | 33 | #include "usb.h" |
Rafaël Carré | b812465 | 2010-06-18 01:47:39 +0000 | [diff] [blame] | 34 | #include "sysfont.h" |
Amaury Pouly | 761e798 | 2015-01-08 22:30:22 +0100 | [diff] [blame] | 35 | #endif /* HAVE_BOOTLOADER_USB_MODE */ |
Rafaël Carré | 3520d8e | 2009-06-17 19:55:27 +0000 | [diff] [blame] | 36 | #include "backlight.h" |
Jonathan Gordon | ce505b8 | 2008-11-11 11:01:55 +0000 | [diff] [blame] | 37 | #include "button-target.h" |
Dave Chapman | 8df1a7e | 2008-10-29 01:42:03 +0000 | [diff] [blame] | 38 | #include "common.h" |
Marcin Bukat | 0b29691 | 2012-03-04 15:34:29 +0100 | [diff] [blame] | 39 | #include "rb-loader.h" |
| 40 | #include "loader_strerror.h" |
Frank Gevaerts | 2f8a008 | 2008-11-01 16:14:28 +0000 | [diff] [blame] | 41 | #include "storage.h" |
Michael Sevakis | 7d1a47c | 2013-08-05 22:02:45 -0400 | [diff] [blame] | 42 | #include "file_internal.h" |
Rafaël Carré | aef27e1 | 2008-11-09 06:17:21 +0000 | [diff] [blame] | 43 | #include "disk.h" |
| 44 | #include "panic.h" |
Mustapha Senhaji | aeddacb | 2009-04-10 22:18:02 +0000 | [diff] [blame] | 45 | #include "power.h" |
Dave Chapman | da8bff1 | 2008-10-12 16:46:01 +0000 | [diff] [blame] | 46 | |
Bertrik Sikken | 7e3a3f4 | 2011-09-08 18:31:15 +0000 | [diff] [blame] | 47 | void show_logo(void); |
Rafaël Carré | f504153 | 2009-06-08 23:05:33 +0000 | [diff] [blame] | 48 | |
Amaury Pouly | 761e798 | 2015-01-08 22:30:22 +0100 | [diff] [blame] | 49 | #ifdef HAVE_BOOTLOADER_USB_MODE |
Rafaël Carré | 1ec8212 | 2010-06-23 05:08:36 +0000 | [diff] [blame] | 50 | static void usb_mode(void) |
| 51 | { |
| 52 | if(usb_detect() != USB_INSERTED) |
| 53 | { |
Rafaël Carré | 8c954e2 | 2011-07-02 02:49:20 +0000 | [diff] [blame] | 54 | static const char msg[] = "Plug USB cable"; |
Rafaël Carré | 1ec8212 | 2010-06-23 05:08:36 +0000 | [diff] [blame] | 55 | reset_screen(); |
| 56 | lcd_putsxy( (LCD_WIDTH - (SYSFONT_WIDTH * sizeof(msg))) / 2, |
| 57 | (LCD_HEIGHT - SYSFONT_HEIGHT) / 2, msg); |
| 58 | lcd_update(); |
| 59 | |
| 60 | /* wait until USB is plugged */ |
| 61 | while(usb_detect() != USB_INSERTED) ; |
| 62 | } |
| 63 | |
Rafaël Carré | 8c954e2 | 2011-07-02 02:49:20 +0000 | [diff] [blame] | 64 | static const char msg[] = "Bootloader USB mode"; |
Rafaël Carré | 1ec8212 | 2010-06-23 05:08:36 +0000 | [diff] [blame] | 65 | reset_screen(); |
| 66 | lcd_putsxy( (LCD_WIDTH - (SYSFONT_WIDTH * sizeof(msg))) / 2, |
| 67 | (LCD_HEIGHT - SYSFONT_HEIGHT) / 2, msg); |
| 68 | lcd_update(); |
| 69 | |
| 70 | while(usb_detect() == USB_INSERTED) |
| 71 | sleep(HZ); |
| 72 | |
| 73 | reset_screen(); |
| 74 | lcd_update(); |
| 75 | } |
Amaury Pouly | 761e798 | 2015-01-08 22:30:22 +0100 | [diff] [blame] | 76 | #endif /* HAVE_BOOTLOADER_USB_MODE */ |
Rafaël Carré | 1ec8212 | 2010-06-23 05:08:36 +0000 | [diff] [blame] | 77 | |
Thomas Martitz | 0e2286f | 2010-08-12 13:38:25 +0000 | [diff] [blame] | 78 | void main(void) NORETURN_ATTR; |
Dave Chapman | da8bff1 | 2008-10-12 16:46:01 +0000 | [diff] [blame] | 79 | void main(void) |
| 80 | { |
Rafaël Carré | aef27e1 | 2008-11-09 06:17:21 +0000 | [diff] [blame] | 81 | unsigned char* loadbuffer; |
| 82 | int buffer_size; |
| 83 | void(*kernel_entry)(void); |
| 84 | int ret; |
Bertrik Sikken | 95726a5 | 2008-10-26 13:28:52 +0000 | [diff] [blame] | 85 | |
Rafaël Carré | b3ee07c | 2008-10-28 11:24:29 +0000 | [diff] [blame] | 86 | system_init(); |
Rafaël Carré | 6e645cc | 2008-11-06 15:24:19 +0000 | [diff] [blame] | 87 | kernel_init(); |
Rafaël Carré | b3ee07c | 2008-10-28 11:24:29 +0000 | [diff] [blame] | 88 | |
Rafaël Carré | 3520d8e | 2009-06-17 19:55:27 +0000 | [diff] [blame] | 89 | enable_irq(); |
| 90 | |
Dave Chapman | 8df1a7e | 2008-10-29 01:42:03 +0000 | [diff] [blame] | 91 | lcd_init(); |
Dave Chapman | da8bff1 | 2008-10-12 16:46:01 +0000 | [diff] [blame] | 92 | show_logo(); |
| 93 | |
Rafaël Carré | 3520d8e | 2009-06-17 19:55:27 +0000 | [diff] [blame] | 94 | backlight_init(); |
Dave Chapman | 8df1a7e | 2008-10-29 01:42:03 +0000 | [diff] [blame] | 95 | |
Rafaël Carré | 2b39cb4 | 2008-11-25 15:43:38 +0000 | [diff] [blame] | 96 | button_init_device(); |
Jonathan Gordon | ce505b8 | 2008-11-11 11:01:55 +0000 | [diff] [blame] | 97 | int btn = button_read_device(); |
| 98 | |
Rafaël Carré | 12af292 | 2010-02-05 12:40:25 +0000 | [diff] [blame] | 99 | #if !defined(SANSA_FUZE) && !defined(SANSA_CLIP) && !defined(SANSA_CLIPV2) \ |
Bertrik Sikken | 463b3ed | 2011-08-27 16:21:19 +0000 | [diff] [blame] | 100 | && !defined(SANSA_CLIPPLUS) && !defined(SANSA_CLIPZIP) |
Mustapha Senhaji | aeddacb | 2009-04-10 22:18:02 +0000 | [diff] [blame] | 101 | if (button_hold()) |
| 102 | { |
| 103 | verbose = true; |
| 104 | lcd_clear_display(); |
| 105 | printf("Hold switch on"); |
| 106 | printf("Shutting down..."); |
| 107 | sleep(HZ); |
| 108 | power_off(); |
| 109 | } |
| 110 | #endif |
| 111 | |
Jonathan Gordon | ce505b8 | 2008-11-11 11:01:55 +0000 | [diff] [blame] | 112 | /* Enable bootloader messages if any button is pressed */ |
| 113 | if (btn) |
Jonathan Gordon | ce505b8 | 2008-11-11 11:01:55 +0000 | [diff] [blame] | 114 | { |
| 115 | lcd_clear_display(); |
| 116 | verbose = true; |
| 117 | } |
Rafaël Carré | aef27e1 | 2008-11-09 06:17:21 +0000 | [diff] [blame] | 118 | |
Rafaël Carré | aef27e1 | 2008-11-09 06:17:21 +0000 | [diff] [blame] | 119 | ret = storage_init(); |
| 120 | if(ret < 0) |
Rafaël Carré | 1ec8212 | 2010-06-23 05:08:36 +0000 | [diff] [blame] | 121 | error(EATA, ret, true); |
Rafaël Carré | aef27e1 | 2008-11-09 06:17:21 +0000 | [diff] [blame] | 122 | |
Michael Sevakis | 7d1a47c | 2013-08-05 22:02:45 -0400 | [diff] [blame] | 123 | filesystem_init(); |
| 124 | |
Amaury Pouly | 761e798 | 2015-01-08 22:30:22 +0100 | [diff] [blame] | 125 | #ifdef HAVE_BOOTLOADER_USB_MODE |
Rafaël Carré | b812465 | 2010-06-18 01:47:39 +0000 | [diff] [blame] | 126 | usb_init(); |
Rafaël Carré | 1ec8212 | 2010-06-23 05:08:36 +0000 | [diff] [blame] | 127 | |
| 128 | /* Enter USB mode if USB is plugged and SELECT button is pressed */ |
Mihail Zenkov | 8b8b854 | 2014-09-29 22:05:25 +0000 | [diff] [blame] | 129 | if(btn & BUTTON_SELECT) { |
| 130 | usb_start_monitoring(); |
| 131 | if(usb_detect() == USB_INSERTED) |
| 132 | usb_mode(); |
| 133 | } |
Amaury Pouly | 761e798 | 2015-01-08 22:30:22 +0100 | [diff] [blame] | 134 | #endif /* HAVE_BOOTLOADER_USB_MODE */ |
Rafaël Carré | 1ec8212 | 2010-06-23 05:08:36 +0000 | [diff] [blame] | 135 | |
Rafaël Carré | 1ec8212 | 2010-06-23 05:08:36 +0000 | [diff] [blame] | 136 | while((ret = disk_mount_all()) <= 0) |
Rafaël Carré | b812465 | 2010-06-18 01:47:39 +0000 | [diff] [blame] | 137 | { |
Amaury Pouly | 761e798 | 2015-01-08 22:30:22 +0100 | [diff] [blame] | 138 | #ifdef HAVE_BOOTLOADER_USB_MODE |
Rafaël Carré | 1ec8212 | 2010-06-23 05:08:36 +0000 | [diff] [blame] | 139 | error(EDISK, ret, false); |
Mihail Zenkov | 2ec6fbc | 2015-06-01 23:41:15 +0000 | [diff] [blame] | 140 | usb_start_monitoring(); |
Rafaël Carré | 1ec8212 | 2010-06-23 05:08:36 +0000 | [diff] [blame] | 141 | usb_mode(); |
Rafaël Carré | 64d3a22 | 2010-06-23 05:16:24 +0000 | [diff] [blame] | 142 | #else |
| 143 | error(EDISK, ret, true); |
| 144 | #endif |
Rafaël Carré | b812465 | 2010-06-18 01:47:39 +0000 | [diff] [blame] | 145 | } |
Rafaël Carré | aef27e1 | 2008-11-09 06:17:21 +0000 | [diff] [blame] | 146 | |
| 147 | printf("Loading firmware"); |
| 148 | |
Thomas Martitz | 4633446 | 2009-05-20 21:09:53 +0000 | [diff] [blame] | 149 | loadbuffer = (unsigned char*)DRAM_ORIG; /* DRAM */ |
| 150 | buffer_size = (int)(loadbuffer + (DRAM_SIZE) - TTB_SIZE); |
Rafaël Carré | aef27e1 | 2008-11-09 06:17:21 +0000 | [diff] [blame] | 151 | |
Marcin Bukat | 0b29691 | 2012-03-04 15:34:29 +0100 | [diff] [blame] | 152 | while((ret = load_firmware(loadbuffer, BOOTFILE, buffer_size)) <= EFILE_EMPTY) |
Rafaël Carré | aef27e1 | 2008-11-09 06:17:21 +0000 | [diff] [blame] | 153 | { |
Amaury Pouly | 761e798 | 2015-01-08 22:30:22 +0100 | [diff] [blame] | 154 | #ifdef HAVE_BOOTLOADER_USB_MODE |
Rafaël Carré | 1ec8212 | 2010-06-23 05:08:36 +0000 | [diff] [blame] | 155 | error(EBOOTFILE, ret, false); |
Mihail Zenkov | 2ec6fbc | 2015-06-01 23:41:15 +0000 | [diff] [blame] | 156 | usb_start_monitoring(); |
Rafaël Carré | 1ec8212 | 2010-06-23 05:08:36 +0000 | [diff] [blame] | 157 | usb_mode(); |
Rafaël Carré | 64d3a22 | 2010-06-23 05:16:24 +0000 | [diff] [blame] | 158 | #else |
| 159 | error(EBOOTFILE, ret, true); |
| 160 | #endif |
Bertrik Sikken | 95726a5 | 2008-10-26 13:28:52 +0000 | [diff] [blame] | 161 | } |
Dave Chapman | da8bff1 | 2008-10-12 16:46:01 +0000 | [diff] [blame] | 162 | |
Mihail Zenkov | 7cb1e5a | 2016-04-22 12:17:35 +0000 | [diff] [blame] | 163 | #if defined(SANSA_FUZEV2) || defined(SANSA_CLIPPLUS) || defined(SANSA_CLIPZIP) |
| 164 | /* It is necessary for proper detection AMSv2 variant 1. |
| 165 | * We should restore initial state of GPIOB_PIN(5) as it used for |
| 166 | * variant detection, but can be changed if we switch SD card. */ |
| 167 | if (amsv2_variant == 1) |
| 168 | GPIOB_PIN(5) = 1 << 5; |
| 169 | #endif |
Rafaël Carré | 1ec8212 | 2010-06-23 05:08:36 +0000 | [diff] [blame] | 170 | kernel_entry = (void*) loadbuffer; |
Michael Sevakis | 0976982 | 2011-12-17 07:54:00 +0000 | [diff] [blame] | 171 | commit_discard_idcache(); |
Rafaël Carré | 1ec8212 | 2010-06-23 05:08:36 +0000 | [diff] [blame] | 172 | printf("Executing"); |
| 173 | kernel_entry(); |
| 174 | printf("ERR: Failed to boot"); |
| 175 | |
Dave Chapman | da8bff1 | 2008-10-12 16:46:01 +0000 | [diff] [blame] | 176 | /* never returns */ |
| 177 | while(1) ; |
| 178 | } |