Path: blob/master/arch/cris/boot/rescue/head_v10.S
10819 views
/*1* Rescue code, made to reside at the beginning of the2* flash-memory. when it starts, it checks a partition3* table at the first sector after the rescue sector.4* the partition table was generated by the product builder5* script and contains offsets, lengths, types and checksums6* for each partition that this code should check.7*8* If any of the checksums fail, we assume the flash is so9* corrupt that we can't use it to boot into the ftp flash10* loader, and instead we initialize the serial port to11* receive a flash-loader and new flash image. we dont include12* any flash code here, but just accept a certain amount of13* bytes from the serial port and jump into it. the downloaded14* code is put in the cache.15*16* The partitiontable is designed so that it is transparent to17* code execution - it has a relative branch opcode in the18* beginning that jumps over it. each entry contains extra19* data so we can add stuff later.20*21* Partition table format:22*23* Code transparency:24*25* 2 bytes [opcode 'nop']26* 2 bytes [opcode 'di']27* 4 bytes [opcode 'ba <offset>', 8-bit or 16-bit version]28* 2 bytes [opcode 'nop', delay slot]29*30* Table validation (at +10):31*32* 2 bytes [magic/version word for partitiontable - 0xef, 0xbe]33* 2 bytes [length of all entries plus the end marker]34* 4 bytes [checksum for the partitiontable itself]35*36* Entries, each with the following format, last has offset -1:37*38* 4 bytes [offset in bytes, from start of flash]39* 4 bytes [length in bytes of partition]40* 4 bytes [checksum, simple longword sum]41* 2 bytes [partition type]42* 2 bytes [flags, only bit 0 used, ro/rw = 1/0]43* 16 bytes [reserved for future use]44*45* End marker46*47* 4 bytes [-1]48*49* 10 bytes [0, padding]50*51* Bit 0 in flags signifies RW or RO. The rescue code only bothers52* to check the checksum for RO partitions, since the others will53* change their data without updating the checksums. A 1 in bit 054* means RO, 0 means RW. That way, it is possible to set a partition55* in RO mode initially, and later mark it as RW, since you can always56* write 0's to the flash.57*58* During the wait for serial input, the status LED will flash so the59* user knows something went wrong.60*61* Copyright (C) 1999-2007 Axis Communications AB62*/6364#ifdef CONFIG_ETRAX_AXISFLASHMAP6566#define ASSEMBLER_MACROS_ONLY67#include <arch/sv_addr_ag.h>6869;; The partitiontable is looked for at the first sector after the boot70;; sector. Sector size is 65536 bytes in all flashes we use.7172#define PTABLE_START CONFIG_ETRAX_PTABLE_SECTOR73#define PTABLE_MAGIC 0xbeef7475;; The normal Etrax100 on-chip boot ROM does serial boot at 0x380000f0.76;; That is not where we put our downloaded serial boot-code.77;; The length is enough for downloading code that loads the rest78;; of itself (after having setup the DRAM etc).79;; It is the same length as the on-chip ROM loads, so the same80;; host loader can be used to load a rescued product as well as81;; one booted through the Etrax serial boot code.8283#define CODE_START 0x4000000084#define CODE_LENGTH 7848586#ifdef CONFIG_ETRAX_RESCUE_SER087#define SERXOFF R_SERIAL0_XOFF88#define SERBAUD R_SERIAL0_BAUD89#define SERRECC R_SERIAL0_REC_CTRL90#define SERRDAT R_SERIAL0_REC_DATA91#define SERSTAT R_SERIAL0_STATUS92#endif93#ifdef CONFIG_ETRAX_RESCUE_SER194#define SERXOFF R_SERIAL1_XOFF95#define SERBAUD R_SERIAL1_BAUD96#define SERRECC R_SERIAL1_REC_CTRL97#define SERRDAT R_SERIAL1_REC_DATA98#define SERSTAT R_SERIAL1_STATUS99#endif100#ifdef CONFIG_ETRAX_RESCUE_SER2101#define SERXOFF R_SERIAL2_XOFF102#define SERBAUD R_SERIAL2_BAUD103#define SERRECC R_SERIAL2_REC_CTRL104#define SERRDAT R_SERIAL2_REC_DATA105#define SERSTAT R_SERIAL2_STATUS106#endif107#ifdef CONFIG_ETRAX_RESCUE_SER3108#define SERXOFF R_SERIAL3_XOFF109#define SERBAUD R_SERIAL3_BAUD110#define SERRECC R_SERIAL3_REC_CTRL111#define SERRDAT R_SERIAL3_REC_DATA112#define SERSTAT R_SERIAL3_STATUS113#endif114115#define NOP_DI 0xf025050f116#define RAM_INIT_MAGIC 0x56902387117118.text119120;; This is the entry point of the rescue code121;; 0x80000000 if loaded in flash (as it should be)122;; Since etrax actually starts at address 2 when booting from flash, we123;; put a nop (2 bytes) here first so we dont accidentally skip the di124125nop126di127128jump in_cache ; enter cached area instead129in_cache:130131132;; First put a jump test to give a possibility of upgrading the133;; rescue code without erasing/reflashing the sector.134;; We put a longword of -1 here and if it is not -1, we jump using135;; the value as jump target. Since we can always change 1's to 0's136;; without erasing the sector, it is possible to add new137;; code after this and altering the jumptarget in an upgrade.138139jtcd: move.d [jumptarget], $r0140cmp.d 0xffffffff, $r0141beq no_newjump142nop143144jump [$r0]145146jumptarget:147.dword 0xffffffff ; can be overwritten later to insert new code148149no_newjump:150#ifdef CONFIG_ETRAX_ETHERNET151;; Start MII clock to make sure it is running when tranceiver is reset152move.d 0x3, $r0 ; enable = on, phy = mii_clk153move.d $r0, [R_NETWORK_GEN_CONFIG]154#endif155156;; We need to setup the bus registers before we start using the DRAM157#include "../../../arch-v10/lib/dram_init.S"158159;; we now should go through the checksum-table and check the listed160;; partitions for errors.161162move.d PTABLE_START, $r3163move.d [$r3], $r0164cmp.d NOP_DI, $r0 ; make sure the nop/di is there...165bne do_rescue166nop167168;; skip the code transparency block (10 bytes).169170addq 10, $r3171172;; check for correct magic173174move.w [$r3+], $r0175cmp.w PTABLE_MAGIC, $r0176bne do_rescue ; didn't recognize - trig rescue177nop178179;; check for correct ptable checksum180181movu.w [$r3+], $r2 ; ptable length182move.d $r2, $r8 ; save for later, length of total ptable183addq 28, $r8 ; account for the rest184move.d [$r3+], $r4 ; ptable checksum185move.d $r3, $r1186jsr checksum ; r1 source, r2 length, returns in r0187188cmp.d $r0, $r4189bne do_rescue ; didn't match - trig rescue190nop191192;; ptable is ok. validate each entry.193194moveq -1, $r7195196ploop: move.d [$r3+], $r1 ; partition offset (from ptable start)197bne notfirst ; check if it is the partition containing ptable198nop ; yes..199move.d $r8, $r1 ; for its checksum check, skip the ptable200move.d [$r3+], $r2 ; partition length201sub.d $r8, $r2 ; minus the ptable length202ba bosse203nop204notfirst:205cmp.d -1, $r1 ; the end of the ptable ?206beq flash_ok ; if so, the flash is validated207move.d [$r3+], $r2 ; partition length208bosse: move.d [$r3+], $r5 ; checksum209move.d [$r3+], $r4 ; type and flags210addq 16, $r3 ; skip the reserved bytes211btstq 16, $r4 ; check ro flag212bpl ploop ; rw partition, skip validation213nop214btstq 17, $r4 ; check bootable flag215bpl 1f216nop217move.d $r1, $r7 ; remember boot partition offset2181:219add.d PTABLE_START, $r1220221jsr checksum ; checksum the partition222223cmp.d $r0, $r5224beq ploop ; checksums matched, go to next entry225nop226227;; otherwise fall through to the rescue code.228229do_rescue:230;; setup port PA and PB default initial directions and data231;; (so we can flash LEDs, and so that DTR and others are set)232233move.b CONFIG_ETRAX_DEF_R_PORT_PA_DIR, $r0234move.b $r0, [R_PORT_PA_DIR]235move.b CONFIG_ETRAX_DEF_R_PORT_PA_DATA, $r0236move.b $r0, [R_PORT_PA_DATA]237238move.b CONFIG_ETRAX_DEF_R_PORT_PB_DIR, $r0239move.b $r0, [R_PORT_PB_DIR]240move.b CONFIG_ETRAX_DEF_R_PORT_PB_DATA, $r0241move.b $r0, [R_PORT_PB_DATA]242243;; setup the serial port at 115200 baud244245moveq 0, $r0246move.d $r0, [SERXOFF]247248move.b 0x99, $r0249move.b $r0, [SERBAUD] ; 115.2kbaud for both transmit and receive250251move.b 0x40, $r0 ; rec enable252move.b $r0, [SERRECC]253254moveq 0, $r1 ; "timer" to clock out a LED red flash255move.d CODE_START, $r3 ; destination counter256movu.w CODE_LENGTH, $r4; length257258wait_ser:259addq 1, $r1260#ifndef CONFIG_ETRAX_NO_LEDS261#ifdef CONFIG_ETRAX_PA_LEDS262move.b CONFIG_ETRAX_DEF_R_PORT_PA_DATA, $r2263#endif264#ifdef CONFIG_ETRAX_PB_LEDS265move.b CONFIG_ETRAX_DEF_R_PORT_PB_DATA, $r2266#endif267move.d (1 << CONFIG_ETRAX_LED1R) | (1 << CONFIG_ETRAX_LED2R), $r0268btstq 16, $r1269bpl 1f270nop271or.d $r0, $r2 ; set bit272ba 2f273nop2741: not $r0 ; clear bit275and.d $r0, $r22762:277#ifdef CONFIG_ETRAX_PA_LEDS278move.b $r2, [R_PORT_PA_DATA]279#endif280#ifdef CONFIG_ETRAX_PB_LEDS281move.b $r2, [R_PORT_PB_DATA]282#endif283#ifdef CONFIG_ETRAX_90000000_LEDS284move.b $r2, [0x90000000]285#endif286#endif287288;; check if we got something on the serial port289290move.b [SERSTAT], $r0291btstq 0, $r0 ; data_avail292bpl wait_ser293nop294295;; got something - copy the byte and loop296297move.b [SERRDAT], $r0298move.b $r0, [$r3+]299300subq 1, $r4 ; decrease length301bne wait_ser302nop303304;; jump into downloaded code305306move.d RAM_INIT_MAGIC, $r8 ; Tell next product that DRAM is307; initialized308jump CODE_START309310flash_ok:311;; check r7, which contains either -1 or the partition to boot from312313cmp.d -1, $r7314bne 1f315nop316move.d PTABLE_START, $r7; otherwise use the ptable start3171:318move.d RAM_INIT_MAGIC, $r8 ; Tell next product that DRAM is319; initialized320jump $r7 ; boot!321322323;; Helper subroutines324325;; Will checksum by simple addition326;; r1 - source327;; r2 - length in bytes328;; result will be in r0329checksum:330moveq 0, $r0331moveq CONFIG_ETRAX_FLASH1_SIZE, $r6332333;; If the first physical flash memory is exceeded wrap to the334;; second one335btstq 26, $r1 ; Are we addressing first flash?336bpl 1f337nop338clear.d $r63393401: test.d $r6 ; 0 = no wrapping341beq 2f342nop343lslq 20, $r6 ; Convert MB to bytes344sub.d $r1, $r63453462: addu.b [$r1+], $r0347subq 1, $r6 ; Flash memory left348beq 3f349subq 1, $r2 ; Length left350bne 2b351nop352ret353nop3543553: move.d MEM_CSE1_START, $r1 ; wrap to second flash356ba 2b357nop358359#endif360361362