imgrecv.c 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. /*
  2. * raspi/imgrecv.c
  3. * https://gitlab.com/bztsrc/imgrecv
  4. *
  5. * Copyright (C) 2020 bzt (bztsrc@github)
  6. *
  7. * Permission is hereby granted, free of charge, to any person
  8. * obtaining a copy of this software and associated documentation
  9. * files (the "Software"), to deal in the Software without
  10. * restriction, including without limitation the rights to use, copy,
  11. * modify, merge, publish, distribute, sublicense, and/or sell copies
  12. * of the Software, and to permit persons to whom the Software is
  13. * furnished to do so, subject to the following conditions:
  14. *
  15. * The above copyright notice and this permission notice shall be
  16. * included in all copies or substantial portions of the Software.
  17. *
  18. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  19. * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  20. * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  21. * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
  22. * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
  23. * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  24. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
  25. * DEALINGS IN THE SOFTWARE.
  26. *
  27. */
  28. #ifndef MMIO_BASE
  29. # error "Define MMIO_BASE for this SoC"
  30. #endif
  31. #define GPFSEL0 ((volatile unsigned int*)(MMIO_BASE+0x00200000))
  32. #define GPFSEL1 ((volatile unsigned int*)(MMIO_BASE+0x00200004))
  33. #define GPFSEL2 ((volatile unsigned int*)(MMIO_BASE+0x00200008))
  34. #define GPFSEL3 ((volatile unsigned int*)(MMIO_BASE+0x0020000C))
  35. #define GPFSEL4 ((volatile unsigned int*)(MMIO_BASE+0x00200010))
  36. #define GPFSEL5 ((volatile unsigned int*)(MMIO_BASE+0x00200014))
  37. #define GPSET0 ((volatile unsigned int*)(MMIO_BASE+0x0020001C))
  38. #define GPSET1 ((volatile unsigned int*)(MMIO_BASE+0x00200020))
  39. #define GPCLR0 ((volatile unsigned int*)(MMIO_BASE+0x00200028))
  40. #define GPLEV0 ((volatile unsigned int*)(MMIO_BASE+0x00200034))
  41. #define GPLEV1 ((volatile unsigned int*)(MMIO_BASE+0x00200038))
  42. #define GPEDS0 ((volatile unsigned int*)(MMIO_BASE+0x00200040))
  43. #define GPEDS1 ((volatile unsigned int*)(MMIO_BASE+0x00200044))
  44. #define GPHEN0 ((volatile unsigned int*)(MMIO_BASE+0x00200064))
  45. #define GPHEN1 ((volatile unsigned int*)(MMIO_BASE+0x00200068))
  46. #define GPPUD ((volatile unsigned int*)(MMIO_BASE+0x00200094))
  47. #define GPPUDCLK0 ((volatile unsigned int*)(MMIO_BASE+0x00200098))
  48. #define GPPUDCLK1 ((volatile unsigned int*)(MMIO_BASE+0x0020009C))
  49. /* PL011 UART registers */
  50. #define UART0_DR ((volatile unsigned int*)(MMIO_BASE+0x00201000))
  51. #define UART0_FR ((volatile unsigned int*)(MMIO_BASE+0x00201018))
  52. #define UART0_IBRD ((volatile unsigned int*)(MMIO_BASE+0x00201024))
  53. #define UART0_FBRD ((volatile unsigned int*)(MMIO_BASE+0x00201028))
  54. #define UART0_LCRH ((volatile unsigned int*)(MMIO_BASE+0x0020102C))
  55. #define UART0_CR ((volatile unsigned int*)(MMIO_BASE+0x00201030))
  56. #define UART0_IMSC ((volatile unsigned int*)(MMIO_BASE+0x00201038))
  57. #define UART0_ICR ((volatile unsigned int*)(MMIO_BASE+0x00201044))
  58. /* AUX UART registers */
  59. #define AUX_ENABLE ((volatile unsigned int*)(MMIO_BASE+0x00215004))
  60. /* mailbox registers */
  61. #define MBOX_READ ((volatile unsigned int*)(MMIO_BASE+0x0000B880+0x0))
  62. #define MBOX_WRITE ((volatile unsigned int*)(MMIO_BASE+0x0000B880+0x20))
  63. #define MBOX_STATUS ((volatile unsigned int*)(MMIO_BASE+0x0000B880+0x18))
  64. #define MBOX_FULL 0x80000000
  65. #define MBOX_EMPTY 0x40000000
  66. #define MBOX_REQUEST 0
  67. #define MBOX_TAG_SETCLKRATE 0x38002
  68. #define MBOX_TAG_LAST 0
  69. /* mailbox message buffer */
  70. volatile unsigned int __attribute__((aligned(16))) mbox[9] = {
  71. 8*4, MBOX_REQUEST, MBOX_TAG_SETCLKRATE, 12, 8, 2, 4000000, 0 ,MBOX_TAG_LAST
  72. };
  73. /**
  74. * Send a character
  75. */
  76. void uart_send(unsigned int c) {
  77. /* wait until we can send */
  78. do{asm volatile("nop");}while(*UART0_FR&0x20);
  79. /* write the character to the buffer */
  80. *UART0_DR=c;
  81. }
  82. /**
  83. * Receive a character
  84. */
  85. char uart_getc() {
  86. /* wait until something is in the buffer */
  87. do{asm volatile("nop");}while(*UART0_FR&0x10);
  88. /* read it and return */
  89. return (char)(*UART0_DR);
  90. }
  91. /**
  92. * Delay cnt clockcycles
  93. */
  94. void static inline delay(int cnt) { while(cnt--) { asm volatile("nop"); } }
  95. /**
  96. * The main receiver function. Gets the load address, returns the entry point
  97. */
  98. char *receiver(char *loadaddr)
  99. {
  100. int size = 0;
  101. char *kernel = loadaddr;
  102. register unsigned int r = (((unsigned int)((unsigned long)&mbox)&~0xF) | 8);
  103. /* initialize UART */
  104. *UART0_CR = 0; /* turn off UART0 */
  105. *AUX_ENABLE = 0; /* turn off UART1 */
  106. /* set up clock for consistent divisor values */
  107. do{asm volatile("nop");}while(*MBOX_STATUS & MBOX_FULL);
  108. *MBOX_WRITE = r;
  109. do {
  110. do{asm volatile("nop");}while(*MBOX_STATUS & MBOX_EMPTY);
  111. } while(r != *MBOX_READ);
  112. /* map UART0 to GPIO pins */
  113. r=*GPFSEL1;
  114. r&=~((7<<12)|(7<<15)); /* gpio14, gpio15 */
  115. r|=(4<<12)|(4<<15); /* alt0 */
  116. *GPFSEL1 = r;
  117. *GPPUD = 0; /* enable pins 14 and 15 */
  118. delay(150);
  119. *GPPUDCLK0 = (1<<14)|(1<<15);
  120. delay(150);
  121. *GPPUDCLK0 = 0; /* flush GPIO setup */
  122. *UART0_ICR = 0x7FF; /* clear interrupts */
  123. *UART0_IBRD = 2; /* 115200 baud */
  124. *UART0_FBRD = 0xB;
  125. *UART0_LCRH = 0x3<<5; /* 8n1 */
  126. *UART0_CR = 0x301; /* enable Tx, Rx, FIFO */
  127. while(1) {
  128. /* notify raspbootcom / USBImager to send the kernel */
  129. uart_send(3);
  130. uart_send(3);
  131. uart_send(3);
  132. /* read the kernel's size */
  133. size = uart_getc();
  134. size |= uart_getc()<<8;
  135. size |= uart_getc()<<16;
  136. size |= uart_getc()<<24;
  137. /* send negative or positive acknowledge */
  138. if(size < 32 || size >= 4*1024*1024) {
  139. /* size error */
  140. uart_send('S');
  141. uart_send('E');
  142. } else {
  143. uart_send('O');
  144. uart_send('K');
  145. break;
  146. }
  147. }
  148. /* read in the image */
  149. while(size--) *kernel++ = uart_getc();
  150. /* get entry point */
  151. return loadaddr[0] == 0x7f && loadaddr[1] == 'E' && loadaddr[2] == 'L' && loadaddr[3] == 'F' ?
  152. #if __WORDSIZE == 64
  153. (char*)(*((unsigned long*)(loadaddr + 24)))
  154. #else
  155. (char*)(*((unsigned int*)(loadaddr + 24)))
  156. #endif
  157. : loadaddr;
  158. }