//------------------------------------------------------------------------ int notmain ( void ) { unsigned int ra; uart_init(); hexstring(0x12345678); hexstring(GETPC()); //e12fff1e bx lr //ee100f10 mrc 15, 0, r0, cr0, cr0, {0} //ee100f30 mrc 15, 0, r0, cr0, cr0, {1} //ee100f50 mrc 15, 0, r0, cr0, cr0, {2} //ee100f70 mrc 15, 0, r0, cr0, cr0, {3} //ee100f90 mrc 15, 0, r0, cr0, cr0, {4} //ee100fb0 mrc 15, 0, r0, cr0, cr0, {5} //ee100fd0 mrc 15, 0, r0, cr0, cr0, {6} //ee100ff0 mrc 15, 0, r0, cr0, cr0, {7} //ee100f11 mrc 15, 0, r0, cr0, cr1, {0} //ee100f31 mrc 15, 0, r0, cr0, cr1, {1} //ee100f51 mrc 15, 0, r0, cr0, cr1, {2} //ee100f71 mrc 15, 0, r0, cr0, cr1, {3} //ee100f91 mrc 15, 0, r0, cr0, cr1, {4} //ee100fb1 mrc 15, 0, r0, cr0, cr1, {5} //ee100fd1 mrc 15, 0, r0, cr0, cr1, {6} //ee100ff1 mrc 15, 0, r0, cr0, cr1, {7} //ee100f12 mrc 15, 0, r0, cr0, cr2, {0} //ee100f32 mrc 15, 0, r0, cr0, cr2, {1} //ee100f52 mrc 15, 0, r0, cr0, cr2, {2} //ee100f72 mrc 15, 0, r0, cr0, cr2, {3} //ee100f92 mrc 15, 0, r0, cr0, cr2, {4} //ee100fb2 mrc 15, 0, r0, cr0, cr2, {5} //ee100fd2 mrc 15, 0, r0, cr0, cr2, {6} //ee100ff2 mrc 15, 0, r0, cr0, cr2, {7} for(ra=0;ra<8;ra++) { PUT32(0x4000,0xee100f10|(ra<<5)); PUT32(0x4004,0xe12fff1e); hexstrings(ra); hexstring(BRANCHTO(0x4000)); } for(ra=0;ra<8;ra++) { PUT32(0x4000,0xee100f11|(ra<<5)); PUT32(0x4004,0xe12fff1e); hexstrings(ra); hexstring(BRANCHTO(0x4000)); } for(ra=0;ra<8;ra++) { PUT32(0x4000,0xee100f12|(ra<<5)); PUT32(0x4004,0xe12fff1e); hexstrings(ra); hexstring(BRANCHTO(0x4000)); } return(0); }
int main (void) { unsigned int length = 0; unsigned long long int sumcheck = 0; unsigned long long int sum = 0; char *mem = (char*)BASE_ADRESS; printf(" (c) Alexis Marquet\n"); printf("Bootloader ready, code destination = 0x%X\n",(unsigned int)mem); printf("Asking for boot file....... \005 \n"); int l = read(0,&length,sizeof(length)); int m = read(0,mem,length); int s = read(0,&sum,sizeof(sum)); for(uint32_t i = 0; i < length; i++) { sumcheck += mem[i]; } if(sum!=sumcheck) { fprintf(stderr,"Error when checking SUM, %llx != %llx\n",sum, sumcheck); while(1); } printf("about to branch...\n"); BRANCHTO(BASE_ADRESS); return 0; }
void notmain ( void ) { uart_init(); hexstring(0x12345678); hexstring(GETPC()); timer_init(); /* * 132 byte packet. All fields are 1 byte except for the 128 byte data * payload. * +-----+------+----------+--....----+-----+ * | SOH | blk# | 255-blk# | ..data.. | cksum | * +-----+------+----------+--....----+-----+ * Protocol: * - first block# = 1. * - CRC is over the whole packet * - after all packets sent, sender transmits a single EOT (must ACK). */ unsigned char block = 1; unsigned addr = ARMBASE; while (1) { unsigned char b; // We received an EOT, send an ACK, jump to beginning of code if((b = getbyte()) == EOT) { uart_send(ACK); BRANCHTO(ARMBASE); return; // NOTREACHED } /* * if first byte is not SOH, or second byte is not the * expected block number or the third byte is not its * negation, send a nak for a resend of this block. */ if(b != SOH || getbyte() != block || getbyte() != (0xFF - block)) { uart_send(NAK); continue; } // get the data bytes int i; unsigned char cksum; for(cksum = i = 0; i < PAYLOAD_SIZE; i++) { cksum += (b = getbyte()); PUT8(addr+i, b); } // Checksum failed: NAK the block if(getbyte() != cksum) uart_send(NAK); // Commit our addr pointer and go to next block. else { uart_send(ACK); addr += PAYLOAD_SIZE; block++; } } }
void interactive_kernel_loop() { char *buff = (char*) 0x10000; uint32_t len = 0x20000 - 0x10000; char *decodebuff = (char*) 0x20000; uint32_t decodebufflen = 0x30000 - 0x20000; int status = 0; while (1) { status = uart_getln(buff, len); if (status == 0) { uart_puts(uart_newline); if (str_startswith(buff, "b64 ")) { uint32_t bytes_decoded = b64_decode(buff+4, decodebuff, decodebufflen); uart_puts("base64 decoded #bytes: "); char tmp[32]; uint32_t tmplen = ARR_LEN(tmp); uart_puts(str_int_to_str(bytes_decoded, tmp, tmplen)); uart_puts(uart_newline); // Copy the code of bootstrap_decoded_binary somewhere safe. uint32_t func_len = 64; // wild guess mem_cpy((uint32_t)bootstrap_decoded_binary, 0x30000, func_len); // Call bootstrap_decoded_binary from that safe location BRANCHTO(0x30000); } else if (str_startswith(buff, "m ")) { inspect_memory(buff+2); } else if (str_startswith(buff, "r ")) { inspect_reg(buff+2); } else if (str_startswith(buff, "icky")) { uart_puts(yoo); } else if (str_startswith(buff, "usr0")) { if (pr0) { switch_to_user_process(pr0); } } else if (str_startswith(buff, "freloc")) { char tmp[32]; uint32_t tmplen = ARR_LEN(tmp); uint32_t func_len = ((uint32_t) str_parse_int) - ((uint32_t) str_len); mem_cpy((uint32_t)str_len, 0x30000, func_len); uart_puts(str_int_to_str(CALL_1(0x30000, "xyz"), tmp, tmplen)); } else if (str_startswith(buff, "version")) { uart_puts(version); uart_puts("\r\n"); } else { int strlen = str_len(buff) - 1; int j = 0; for (; strlen != -1; --strlen, ++j) { decodebuff[j] = buff[strlen]; } decodebuff[j] = 0; uart_puts(decodebuff); } } uart_puts(uart_newline); } }
int notmain ( void ) { unsigned int ra; unsigned int rx; unsigned int block; unsigned int state; unsigned int addr; unsigned int crc; int i = 0; // Initialize mini UART uart_init(); timer_init(); display_msg(); //SOH 0x01 //ACK 0x06 //NAK 0x15 //EOT 0x04 //block numbers start with 1 //132 byte packet //starts with SOH //block number byte //255-block number //128 bytes of data //checksum byte (whole packet) //a single EOT instead of SOH when done, send an ACK on it too block = 1; state = 0; addr = ARMBASE; crc = 0; rx = timer_tick(); while(1) { ra = timer_tick(); if((ra -rx) >= 4000000) { uart_putc(0x15); rx += 4000000; } if (uart_get_flags() & (1 << 4)) continue; xstring[state] = uart_getc(); rx = timer_tick(); if(state == 0) { if(xstring[state] == 0x04) { uart_putc(0x06); BRANCHTO(ARMBASE); break; } } switch(state) { case 0: { if(xstring[state] == 0x01) { crc = xstring[state]; state++; } else { uart_putc(0x15); } break; } case 1: { if(xstring[state] == block) { crc += xstring[state]; state++; } else { state = 0; uart_putc(0x15); } break; } case 2: { if(xstring[state] == (0xFF - xstring[state-1])) { crc += xstring[state]; state++; } else { uart_putc(0x15); state = 0; } break; } case 131: { crc &= 0xFF; if(xstring[state] == crc) { for(ra = 0 ; ra < 128 ; ra++) { PUT8(addr++, xstring[ra+3]); } uart_putc(0x06); block = (block + 1) & 0xFF; } else { uart_putc(0x15); } state = 0; break; } default: { crc += xstring[state]; state++; break; } } } return(0); }
//------------------------------------------------------------------------ int notmain ( void ) { unsigned int state; unsigned int byte_count; unsigned int address; unsigned int record_type; unsigned int segment; unsigned int data; unsigned int sum; unsigned int ra; leds_off(); uart_init(); hexstring(0x12345678); hexstring(GETPC()); uart_send('I'); uart_send('H'); uart_send('E'); uart_send('X'); uart_send(0x0D); uart_send(0x0A); state=0; segment=0; sum=0; data=0; record_type=0; address=0; byte_count=0; while(1) { ra=uart_recv(); if(ra==':') { state=1; continue; } if(ra==0x0D) { state=0; continue; } if(ra==0x0A) { state=0; continue; } if((ra=='g')||(ra=='G')) { uart_send(0x0D); uart_send('-'); uart_send('-'); uart_send(0x0D); uart_send(0x0A); uart_send(0x0A); BRANCHTO(0x8000); state=0; break; } switch(state) { case 0: { break; } case 1: case 2: { byte_count<<=4; if(ra>0x39) ra-=7; byte_count|=(ra&0xF); byte_count&=0xFF; state++; break; } case 3: case 4: case 5: case 6: { address<<=4; if(ra>0x39) ra-=7; address|=(ra&0xF); address&=0xFFFF; address|=segment; state++; break; } case 7: { record_type<<=4; if(ra>0x39) ra-=7; record_type|=(ra&0xF); record_type&=0xFF; state++; break; } case 8: { record_type<<=4; if(ra>0x39) ra-=7; record_type|=(ra&0xF); record_type&=0xFF; switch(record_type) { case 0x00: { state=14; break; } case 0x01: { hexstring(sum); state=0; break; } case 0x02: { state=9; break; } default: { state=0; break; } } break; } case 9: case 10: case 11: case 12: { segment<<=4; if(ra>0x39) ra-=7; segment|=(ra&0xF); segment&=0xFFFF; state++; break; } case 13: { segment<<=4; state=0; break; } case 14: case 15: case 16: case 17: case 18: case 19: case 20: case 21: { data<<=4; if(ra>0x39) ra-=7; data|=(ra&0xF); if(state==21) { ra=(data>>24)|(data<<24); ra|=(data>>8)&0x0000FF00; ra|=(data<<8)&0x00FF0000; data=ra; PUT32(address,data); sum+=address; sum+=data; address+=4; state=14; } else { state++; } break; } }
//------------------------------------------------------------------------ int notmain ( void ) { unsigned int ra; //unsigned int rb; unsigned int rx; unsigned int addr; unsigned int block; unsigned int crc; PUT32(AUX_ENABLES,1); PUT32(AUX_MU_IER_REG,0); PUT32(AUX_MU_CNTL_REG,0); PUT32(AUX_MU_LCR_REG,3); PUT32(AUX_MU_MCR_REG,0); PUT32(AUX_MU_IER_REG,0); PUT32(AUX_MU_IIR_REG,0xC6); PUT32(AUX_MU_BAUD_REG,270); ra=GET32(GPFSEL1); ra&=~(7<<12); //gpio14 ra|=2<<12; //alt5 ra&=~(7<<15); //gpio15 ra|=2<<15; //alt5 PUT32(GPFSEL1,ra); PUT32(GPPUD,0); for(ra=0;ra<150;ra++) dummy(ra); PUT32(GPPUDCLK0,(1<<14)|(1<<15)); for(ra=0;ra<150;ra++) dummy(ra); PUT32(GPPUDCLK0,0); PUT32(AUX_MU_CNTL_REG,3); PUT32(ARM_TIMER_CTL,0x00F90000); PUT32(ARM_TIMER_CTL,0x00F90200); hexstring(0x12345678); //SOH 0x01 //ACK 0x06 //NAK 0x15 //EOT 0x04 //block numbers start with 1 //132 byte packet //starts with SOH //block number byte //255-block number //128 bytes of data //checksum byte (whole packet) //a single EOT instead of SOH when done, send an ACK on it too //this is a very crude solution, worked for a small test program though //if it slips one byte it is all over. Need to make a more robust //solution. rx=GET32(ARM_TIMER_CNT); while(1) { ra=GET32(ARM_TIMER_CNT); if((ra-rx)>=4000000) { uart_send(0x15); rx+=4000000; } if(GET32(AUX_MU_LSR_REG)&0x01) break; } block=1; addr=ARMBASE; while(1) { xstring[0]=uart_recv(); if(xstring[0]==0x04) { uart_send(0x06); break; } if(xstring[0]!=0x01) break; crc=0x01; for(ra=1;ra<132;ra++) { xstring[ra]=uart_recv(); crc+=xstring[ra]; } if(xstring[2]!=(255-xstring[1])) break; crc-=xstring[131]; crc&=0xFF; if(xstring[131]!=crc) { uart_send(0x15); } for(ra=0;ra<128;ra++) { PUT8(addr++,xstring[ra+3]); } if(addr>0x200000) { uart_send(0x15); break; } uart_send(0x06); block=(block+1)&0xFF; } if(xstring[0]==0x04) { BRANCHTO(ARMBASE); } return(0); }