Part of Slepp's ProjectsPastebinTURLImagebinFilebin
Feedback -- English French German Japanese
Create Upload Newest Tools Donate
Sign In | Create Account

Mine
Sunday, November 26th, 2006 at 2:04:53pm UTC 

  1. /***************************************************************************
  2.  *             __________               __   ___.
  3.  *   Open      \______   \ ____   ____ |  | _\_ |__   _______  ___
  4.  *   Source     |       _//  _ \_/ ___\|  |/ /| __ \ /  _ \  \/  /
  5.  *   Jukebox    |    |   (  <_> )  \___|    < | \_\ (  <_> > <  <
  6.  *   Firmware   |____|_  /\____/ \___  >__|_ \|___  /\____/__/\_ \
  7.  *                     \/            \/     \/    \/            \/
  8.  * $Id: h10.c,v 1.4 2006-08-28 08:11:32 barrywardell Exp $
  9.  *
  10.  * Copyright (C) 2006 by Barry Wardell
  11.  *
  12.  * Based on Rockbox iriver bootloader by Linus Nielsen Feltzing
  13.  * and the ipodlinux bootloader by Daniel Palffy and Bernard Leach
  14.  *
  15.  * All files in this archive are subject to the GNU General Public License.
  16.  * See the file COPYING in the source tree root for full license agreement.
  17.  *
  18.  * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
  19.  * KIND, either express or implied.
  20.  *
  21.  ****************************************************************************/
  22. #include "config.h"
  23.  
  24. #include <stdlib.h>
  25. #include <stdio.h>
  26. #include <string.h>
  27. #include "cpu.h"
  28. #include "system.h"
  29. #include "lcd.h"
  30. #include "kernel.h"
  31. #include "thread.h"
  32. #include "ata.h"
  33. #include "fat.h"
  34. #include "disk.h"
  35. #include "font.h"
  36. #include "adc.h"
  37. #include "backlight.h"
  38. #include "button.h"
  39. #include "panic.h"
  40. #include "power.h"
  41. #include "file.h"
  42.  
  43. #define MAX_MENU_COUNT 2
  44. int menu_count=2;
  45. int time_left=5;
  46. int lasttick;
  47. #define MENU_GLOBAL_BORDER 10
  48. #define MENU_GLOBAL_BORDER_Y 30
  49. #define MENU_LINE_HEIGHT 8
  50.  
  51. #define FW_ROCKBOX 0
  52. #define FW_SANDISK 1
  53. struct firmwares
  54. {
  55.         char descr[32];
  56.         char filename[256];
  57.         int fw_type;
  58.         bool exists;
  59. };
  60.        
  61. static struct firmwares  items[MAX_MENU_COUNT] = {{"Sansa","OF.bin",FW_SANDISK,false},\
  62.                                                                             {"Rockbox",BOOTFILE,FW_ROCKBOX,false}};
  63.  
  64.  
  65. /* Size of the buffer to store the loaded Rockbox/Sansa image */
  66. #define MAX_LOADSIZE (10*1024*1024)
  67.  
  68. /* A buffer to load the iriver firmware or Rockbox into */
  69. unsigned char loadbuffer[MAX_LOADSIZE];
  70.  
  71. char version[] = APPSVERSION;
  72.  
  73. /* To store the original state of the GPIOs.
  74. We only use A-F in the bootloader */
  75. unsigned long gpio_en[7];
  76. unsigned long gpio_output_en[7];
  77.  
  78. int curent_line = 0;
  79.  
  80. #define DRAM_START              0x10000000
  81.  
  82. int line=0;
  83.  
  84. /* Save the initial GPIO status from the Sansa bootloader */
  85. void gpio_save(void)
  86. {
  87.     int i = 0;
  88.     gpio_en[i] = GPIOA_ENABLE;
  89.     gpio_output_en[i++] = GPIOA_OUTPUT_EN;
  90.     gpio_en[i] = GPIOB_ENABLE;
  91.     gpio_output_en[i++] = GPIOB_OUTPUT_EN;
  92.     gpio_en[i] = GPIOC_ENABLE;
  93.     gpio_output_en[i++] = GPIOC_OUTPUT_EN;
  94.     gpio_en[i] = GPIOD_ENABLE;
  95.     gpio_output_en[i++] = GPIOD_OUTPUT_EN;
  96.     gpio_en[i] = GPIOE_ENABLE;
  97.     gpio_output_en[i++] = GPIOE_OUTPUT_EN;
  98.     gpio_en[i] = GPIOF_ENABLE;
  99.     gpio_output_en[i++] = GPIOF_OUTPUT_EN;
  100.     gpio_en[i] = GPIOG_ENABLE;
  101.     gpio_output_en[i++] = GPIOG_OUTPUT_EN;
  102. }
  103.  
  104. /* Restore the GPIO status */
  105. void gpio_restore(void)
  106. {
  107.     int i = 0;
  108.     GPIOA_ENABLE = gpio_en[i];
  109.     GPIOA_OUTPUT_EN = gpio_output_en[i++];
  110.     GPIOB_ENABLE = gpio_en[i];
  111.     GPIOB_OUTPUT_EN = gpio_output_en[i++];
  112.     GPIOC_ENABLE = gpio_en[i];
  113.     GPIOC_OUTPUT_EN = gpio_output_en[i++];
  114.     GPIOD_ENABLE = gpio_en[i];
  115.     GPIOD_OUTPUT_EN = gpio_output_en[i++];
  116.     GPIOE_ENABLE = gpio_en[i];
  117.     GPIOE_OUTPUT_EN = gpio_output_en[i++];
  118.     GPIOF_ENABLE = gpio_en[i];
  119.     GPIOF_OUTPUT_EN = gpio_output_en[i++];
  120.     GPIOG_ENABLE = gpio_en[i];
  121.     GPIOG_OUTPUT_EN = gpio_output_en[i++];
  122. }
  123.  
  124. /* Load original iriver firmware. This function expects a file called
  125.    "/System/Original.mi4" on the player. It should be decrypted
  126.    and have the header stripped using mi4code. It reads the file in to a memory
  127.    buffer called buf. The rest of the loading is done in main() and crt0.S
  128. */
  129. int load_iriver(unsigned char* buf)
  130. {
  131.     int fd;
  132.     int rc;
  133.     int len;
  134.  
  135.     fd = open("/.rockbox/OF.bin", O_RDONLY);
  136.  
  137.     len = filesize(fd);
  138.  
  139.     if (len > MAX_LOADSIZE)
  140.         return -6;
  141.  
  142.     rc = read(fd, buf, len);
  143.     if(rc < len)
  144.         return -4;
  145.  
  146.     close(fd);
  147.     gpio_restore();
  148.     return len;
  149. }
  150.  
  151. /* Load Rockbox firmware (rockbox.h10) */
  152. int load_rockbox(unsigned char* buf)
  153. {
  154.     int fd;
  155.     int rc;
  156.     int len;
  157.     unsigned long chksum;
  158.     char model[5];
  159.     unsigned long sum;
  160.     int i;
  161.     char str[80];
  162.  
  163.     fd = open("/.rockbox/" BOOTFILE, O_RDONLY);
  164.     if(fd < 0)
  165.     {
  166.         fd = open("/" BOOTFILE, O_RDONLY);
  167.         if(fd < 0)
  168.             return -1;
  169.     }
  170.  
  171.     len = filesize(fd) - 8;
  172.  
  173.     snprintf(str, sizeof(str), "Length: %x", len);
  174.     lcd_puts(0, line++ ,str);
  175.     lcd_update();
  176.  
  177.     if (len > MAX_LOADSIZE)
  178.         return -6;
  179.  
  180.     lseek(fd, FIRMWARE_OFFSET_FILE_CRC, SEEK_SET);
  181.  
  182.     rc = read(fd, &chksum, 4);
  183.     chksum=betoh32(chksum); /* Rockbox checksums are big-endian */
  184.     if(rc < 4)
  185.         return -2;
  186.  
  187.     snprintf(str, sizeof(str), "Checksum: %x", chksum);
  188.     lcd_puts(0, line++ ,str);
  189.     lcd_update();
  190.  
  191.     rc = read(fd, model, 4);
  192.     if(rc < 4)
  193.         return -3;
  194.  
  195.     model[4] = 0;
  196.  
  197.     snprintf(str, sizeof(str), "Model name: %s", model);
  198.     lcd_puts(0, line++ ,str);
  199.     lcd_update();
  200.  
  201.     lseek(fd, FIRMWARE_OFFSET_FILE_DATA, SEEK_SET);
  202.  
  203.     rc = read(fd, buf, len);
  204.     if(rc < len)
  205.         return -4;
  206.  
  207.     close(fd);
  208.  
  209.     sum = MODEL_NUMBER;
  210.  
  211.     for(i = 0;i < len;i++) {
  212.         sum += buf[i];
  213.     }
  214.  
  215.     /* I don't know why my checksums aren't matching.  Figure this out before committing.  Hack around it for now */
  216.     sum += 4;
  217.  
  218.     snprintf(str, sizeof(str), "Sum: %x", sum);
  219.     lcd_puts(0, line++ ,str);
  220.     lcd_update();
  221.  
  222.     if(sum != chksum)
  223.         return -5;
  224.  
  225.     return len;
  226. }
  227.  
  228.  
  229.  
  230.  
  231. void draw_items(void)
  232. {
  233.         char buf[256];
  234.     int font_h, font_w;
  235.    
  236.         int i;
  237.         lcd_getstringsize((unsigned char *)"A", &font_w, &font_h);
  238.         lcd_clear_display();
  239.         lcd_drawrect(MENU_GLOBAL_BORDER, MENU_GLOBAL_BORDER_Y, LCD_WIDTH-2*MENU_GLOBAL_BORDER, menu_count*MENU_LINE_HEIGHT+4);
  240.     for (i=0;i<menu_count;i++)
  241.     {   
  242.         if (curent_line==i)
  243.         {
  244.             lcd_fillrect((MENU_GLOBAL_BORDER+2),\
  245.                         (MENU_GLOBAL_BORDER_Y+2+i*MENU_LINE_HEIGHT),\
  246.                         LCD_WIDTH-2*(MENU_GLOBAL_BORDER+2),\
  247.                         MENU_LINE_HEIGHT);
  248.             lcd_set_drawmode(DRMODE_COMPLEMENT);
  249.             lcd_putsxy(((LCD_WIDTH)/2)-((strlen(items[i].descr)*font_w)/2),\
  250.                     (MENU_GLOBAL_BORDER_Y+2+i*MENU_LINE_HEIGHT),items[i].descr);
  251.             lcd_set_drawmode(DRMODE_SOLID);
  252.         }
  253.         else
  254.         {
  255.             lcd_putsxy(((LCD_WIDTH)/2)-((strlen(items[i].descr)*font_w)/2),\
  256.                     (MENU_GLOBAL_BORDER_Y+2+i*MENU_LINE_HEIGHT),items[i].descr);
  257.         }
  258.     }
  259.     lcd_drawrect(MENU_GLOBAL_BORDER, (MENU_GLOBAL_BORDER_Y+4+(menu_count)*MENU_LINE_HEIGHT)+MENU_GLOBAL_BORDER,\
  260.                                      LCD_WIDTH-2*MENU_GLOBAL_BORDER, MENU_LINE_HEIGHT+4);
  261.     snprintf(buf, sizeof(buf), "Time left: %d",time_left );
  262.     lcd_putsxy(MENU_GLOBAL_BORDER+2, (MENU_GLOBAL_BORDER_Y+4+(menu_count)*MENU_LINE_HEIGHT)+MENU_GLOBAL_BORDER+2,buf);
  263.     lcd_update();
  264. }
  265.  
  266. void* main(void)
  267. {
  268.     char buf[256];
  269.     int i;
  270.     int rc;
  271.     bool selected = false;
  272.     unsigned short* identify_info;
  273.     struct partinfo* pinfo;
  274.  
  275.     gpio_save();
  276.     system_init();
  277.     kernel_init();
  278.     lcd_init();
  279.     font_init();
  280.    
  281.    /* lcd_setfont(FONT_SYSFIXED);*/
  282.  
  283.  
  284.    
  285.     draw_items();
  286.     lasttick = current_tick;
  287.     time_left = 5;
  288.    
  289.     while (!selected)
  290.     {   sleep(HZ/10);
  291.         if ((current_tick - lasttick)>HZ)
  292.         {
  293.                 time_left--;
  294.                 if (time_left==0)
  295.                 {
  296.                         selected=true;
  297.                 }
  298.                     draw_items();
  299.                     lasttick=current_tick;
  300.         }
  301.        
  302.         i=button_read_device();
  303.         switch (i)
  304.         {
  305.             case BUTTON_UP:
  306.             {
  307.                 curent_line--;
  308.                 if (curent_line<0) curent_line = menu_count -1;   
  309.                
  310.                 draw_items();
  311.                 time_left= -1;
  312.                 break;
  313.             }
  314.            
  315.             case BUTTON_DOWN:
  316.             {
  317.                 curent_line++;
  318.                 if (curent_line>menu_count-1) curent_line = 0
  319.                 draw_items();
  320.                 time_left= -1;
  321.                 break;
  322.             }
  323.             case BUTTON_SELECT:
  324.             {
  325.                 selected = true;
  326.                 break;
  327.             }
  328.        
  329.         }
  330.     }
  331.    
  332.  
  333.     line=0;
  334.  
  335.  
  336.     lcd_puts(0, line++, "Rockbox boot loader");
  337.     snprintf(buf, sizeof(buf), "Version: 20%s", version);
  338.     lcd_puts(0, line++, buf);
  339.     snprintf(buf, sizeof(buf), "iriver H10");
  340.     lcd_puts(0, line++, buf);
  341.     lcd_update();
  342.  
  343.     i=ata_init();
  344.     if (i==0) {
  345.       identify_info=ata_get_identify();
  346.       /* Show model */
  347.       for (i=0; i < 20; i++) {
  348.         ((unsigned short*)buf)[i]=htobe16(identify_info[i+27]);
  349.       }
  350.       buf[40]=0;
  351.       for (i=39; i && buf[i]==' '; i--) {
  352.         buf[i]=0;
  353.       }
  354.       lcd_puts(0, line++, buf);
  355.       lcd_update();
  356.     } else {
  357.       snprintf(buf, sizeof(buf), "ATA: %d", i);
  358.       lcd_puts(0, line++, buf);
  359.       lcd_update();
  360.     }
  361.  
  362.     disk_init();
  363.     rc = disk_mount_all();
  364.     if (rc<=0)
  365.     {
  366.         lcd_puts(0, line++, "No partition found");
  367.         lcd_update();
  368.     }
  369.  
  370.     pinfo = disk_partinfo(0);
  371.     snprintf(buf, sizeof(buf), "Partition 0: 0x%02x %ld MB",
  372.                   pinfo->type, pinfo->size / 2048);
  373.     lcd_puts(0, line++, buf);
  374.     lcd_update();
  375.  
  376.     if(curent_line==0)
  377.     {
  378.         lcd_puts(0, line, "Loading Sansa firmware...");
  379.         lcd_update();
  380.         rc=load_iriver(loadbuffer);
  381.     } else {
  382.         lcd_puts(0, line, "Loading Rockbox...");
  383.         lcd_update();
  384.         rc=load_rockbox(loadbuffer);
  385.     }
  386.  
  387.     if (rc < 0) {
  388.             snprintf(buf, sizeof(buf), "Rockbox error: %d",rc);
  389.             lcd_puts(0, line++, buf);
  390.             lcd_update();
  391.             while(1) {}
  392.     }
  393.  
  394.     memcpy((void*)DRAM_START,loadbuffer,rc);
  395.  
  396.     return (void*)DRAM_START;
  397.    
  398. }
  399.  
  400. /* These functions are present in the firmware library, but we reimplement
  401.    them here because the originals do a lot more than we want */
  402.  
  403. void reset_poweroff_timer(void)
  404. {
  405. }
  406.  
  407. int dbg_ports(void)
  408. {
  409.    return 0;
  410. }
  411.  
  412. void mpeg_stop(void)
  413. {
  414. }
  415.  
  416. void usb_acknowledge(void)
  417. {
  418. }
  419.  
  420. void usb_wait_for_disconnect(void)
  421. {
  422. }
  423.  
  424. void sys_poweroff(void)
  425. {
  426. }

Update the Post

Either update this post and resubmit it with changes, or make a new post.

You may also comment on this post.

update paste below
details of the post (optional)

Note: Only the paste content is required, though the following information can be useful to others.

Save name / title?

(space separated, optional)



Please note that information posted here will expire by default in one month. If you do not want it to expire, please set the expiry time above. If it is set to expire, web search engines will not be allowed to index it prior to it expiring. Items that are not marked to expire will be indexable by search engines. Be careful with your passwords. All illegal activities will be reported and any information will be handed over to the authorities, so be good.

comments powered by Disqus
worth-right
worth-right