kwboot.c 40 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832
  1. /*
  2. * Boot a Marvell SoC, with Xmodem over UART0.
  3. * supports Kirkwood, Dove, Armada 370, Armada XP, Armada 375, Armada 38x and
  4. * Armada 39x
  5. *
  6. * (c) 2012 Daniel Stodden <daniel.stodden@gmail.com>
  7. * (c) 2021 Pali Rohár <pali@kernel.org>
  8. * (c) 2021 Marek Behún <marek.behun@nic.cz>
  9. *
  10. * References: marvell.com, "88F6180, 88F6190, 88F6192, and 88F6281
  11. * Integrated Controller: Functional Specifications" December 2,
  12. * 2008. Chapter 24.2 "BootROM Firmware".
  13. */
  14. #include "kwbimage.h"
  15. #include "mkimage.h"
  16. #include "version.h"
  17. #include <stdlib.h>
  18. #include <stdio.h>
  19. #include <string.h>
  20. #include <stdarg.h>
  21. #include <image.h>
  22. #include <libgen.h>
  23. #include <fcntl.h>
  24. #include <errno.h>
  25. #include <unistd.h>
  26. #include <stdint.h>
  27. #include <time.h>
  28. #include <sys/stat.h>
  29. #ifdef __linux__
  30. #include "termios_linux.h"
  31. #else
  32. #include <termios.h>
  33. #endif
  34. /*
  35. * Marvell BootROM UART Sensing
  36. */
  37. static unsigned char kwboot_msg_boot[] = {
  38. 0xBB, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77
  39. };
  40. static unsigned char kwboot_msg_debug[] = {
  41. 0xDD, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77
  42. };
  43. /* Defines known to work on Kirkwood */
  44. #define KWBOOT_MSG_REQ_DELAY 10 /* ms */
  45. #define KWBOOT_MSG_RSP_TIMEO 50 /* ms */
  46. /* Defines known to work on Armada XP */
  47. #define KWBOOT_MSG_REQ_DELAY_AXP 1000 /* ms */
  48. #define KWBOOT_MSG_RSP_TIMEO_AXP 1000 /* ms */
  49. /*
  50. * Xmodem Transfers
  51. */
  52. #define SOH 1 /* sender start of block header */
  53. #define EOT 4 /* sender end of block transfer */
  54. #define ACK 6 /* target block ack */
  55. #define NAK 21 /* target block negative ack */
  56. #define CAN 24 /* target/sender transfer cancellation */
  57. #define KWBOOT_XM_BLKSZ 128 /* xmodem block size */
  58. struct kwboot_block {
  59. uint8_t soh;
  60. uint8_t pnum;
  61. uint8_t _pnum;
  62. uint8_t data[KWBOOT_XM_BLKSZ];
  63. uint8_t csum;
  64. } __packed;
  65. #define KWBOOT_BLK_RSP_TIMEO 1000 /* ms */
  66. #define KWBOOT_HDR_RSP_TIMEO 10000 /* ms */
  67. /* ARM code to change baudrate */
  68. static unsigned char kwboot_baud_code[] = {
  69. /* ; #define UART_BASE 0xd0012000 */
  70. /* ; #define DLL 0x00 */
  71. /* ; #define DLH 0x04 */
  72. /* ; #define LCR 0x0c */
  73. /* ; #define DLAB 0x80 */
  74. /* ; #define LSR 0x14 */
  75. /* ; #define TEMT 0x40 */
  76. /* ; #define DIV_ROUND(a, b) ((a + b/2) / b) */
  77. /* ; */
  78. /* ; u32 set_baudrate(u32 old_b, u32 new_b) { */
  79. /* ; while */
  80. /* ; (!(readl(UART_BASE + LSR) & TEMT)); */
  81. /* ; u32 lcr = readl(UART_BASE + LCR); */
  82. /* ; writel(UART_BASE + LCR, lcr | DLAB); */
  83. /* ; u8 old_dll = readl(UART_BASE + DLL); */
  84. /* ; u8 old_dlh = readl(UART_BASE + DLH); */
  85. /* ; u16 old_dl = old_dll | (old_dlh << 8); */
  86. /* ; u32 clk = old_b * old_dl; */
  87. /* ; u16 new_dl = DIV_ROUND(clk, new_b); */
  88. /* ; u8 new_dll = new_dl & 0xff; */
  89. /* ; u8 new_dlh = (new_dl >> 8) & 0xff; */
  90. /* ; writel(UART_BASE + DLL, new_dll); */
  91. /* ; writel(UART_BASE + DLH, new_dlh); */
  92. /* ; writel(UART_BASE + LCR, lcr & ~DLAB); */
  93. /* ; msleep(5); */
  94. /* ; return 0; */
  95. /* ; } */
  96. /* ; r0 = UART_BASE */
  97. 0x0d, 0x02, 0xa0, 0xe3, /* mov r0, #0xd0000000 */
  98. 0x12, 0x0a, 0x80, 0xe3, /* orr r0, r0, #0x12000 */
  99. /* ; Wait until Transmitter FIFO is Empty */
  100. /* .Lloop_txempty: */
  101. /* ; r1 = UART_BASE[LSR] & TEMT */
  102. 0x14, 0x10, 0x90, 0xe5, /* ldr r1, [r0, #0x14] */
  103. 0x40, 0x00, 0x11, 0xe3, /* tst r1, #0x40 */
  104. 0xfc, 0xff, 0xff, 0x0a, /* beq .Lloop_txempty */
  105. /* ; Set Divisor Latch Access Bit */
  106. /* ; UART_BASE[LCR] |= DLAB */
  107. 0x0c, 0x10, 0x90, 0xe5, /* ldr r1, [r0, #0x0c] */
  108. 0x80, 0x10, 0x81, 0xe3, /* orr r1, r1, #0x80 */
  109. 0x0c, 0x10, 0x80, 0xe5, /* str r1, [r0, #0x0c] */
  110. /* ; Read current Divisor Latch */
  111. /* ; r1 = UART_BASE[DLH]<<8 | UART_BASE[DLL] */
  112. 0x00, 0x10, 0x90, 0xe5, /* ldr r1, [r0, #0x00] */
  113. 0xff, 0x10, 0x01, 0xe2, /* and r1, r1, #0xff */
  114. 0x01, 0x20, 0xa0, 0xe1, /* mov r2, r1 */
  115. 0x04, 0x10, 0x90, 0xe5, /* ldr r1, [r0, #0x04] */
  116. 0xff, 0x10, 0x01, 0xe2, /* and r1, r1, #0xff */
  117. 0x41, 0x14, 0xa0, 0xe1, /* asr r1, r1, #8 */
  118. 0x02, 0x10, 0x81, 0xe1, /* orr r1, r1, r2 */
  119. /* ; Read old baudrate value */
  120. /* ; r2 = old_baudrate */
  121. 0x74, 0x20, 0x9f, 0xe5, /* ldr r2, old_baudrate */
  122. /* ; Calculate base clock */
  123. /* ; r1 = r2 * r1 */
  124. 0x92, 0x01, 0x01, 0xe0, /* mul r1, r2, r1 */
  125. /* ; Read new baudrate value */
  126. /* ; r2 = new_baudrate */
  127. 0x70, 0x20, 0x9f, 0xe5, /* ldr r2, new_baudrate */
  128. /* ; Calculate new Divisor Latch */
  129. /* ; r1 = DIV_ROUND(r1, r2) = */
  130. /* ; = (r1 + r2/2) / r2 */
  131. 0xa2, 0x10, 0x81, 0xe0, /* add r1, r1, r2, lsr #1 */
  132. 0x02, 0x40, 0xa0, 0xe1, /* mov r4, r2 */
  133. 0xa1, 0x00, 0x54, 0xe1, /* cmp r4, r1, lsr #1 */
  134. /* .Lloop_div1: */
  135. 0x84, 0x40, 0xa0, 0x91, /* movls r4, r4, lsl #1 */
  136. 0xa1, 0x00, 0x54, 0xe1, /* cmp r4, r1, lsr #1 */
  137. 0xfc, 0xff, 0xff, 0x9a, /* bls .Lloop_div1 */
  138. 0x00, 0x30, 0xa0, 0xe3, /* mov r3, #0 */
  139. /* .Lloop_div2: */
  140. 0x04, 0x00, 0x51, 0xe1, /* cmp r1, r4 */
  141. 0x04, 0x10, 0x41, 0x20, /* subhs r1, r1, r4 */
  142. 0x03, 0x30, 0xa3, 0xe0, /* adc r3, r3, r3 */
  143. 0xa4, 0x40, 0xa0, 0xe1, /* mov r4, r4, lsr #1 */
  144. 0x02, 0x00, 0x54, 0xe1, /* cmp r4, r2 */
  145. 0xf9, 0xff, 0xff, 0x2a, /* bhs .Lloop_div2 */
  146. 0x03, 0x10, 0xa0, 0xe1, /* mov r1, r3 */
  147. /* ; Set new Divisor Latch Low */
  148. /* ; UART_BASE[DLL] = r1 & 0xff */
  149. 0x01, 0x20, 0xa0, 0xe1, /* mov r2, r1 */
  150. 0xff, 0x20, 0x02, 0xe2, /* and r2, r2, #0xff */
  151. 0x00, 0x20, 0x80, 0xe5, /* str r2, [r0, #0x00] */
  152. /* ; Set new Divisor Latch High */
  153. /* ; UART_BASE[DLH] = r1>>8 & 0xff */
  154. 0x41, 0x24, 0xa0, 0xe1, /* asr r2, r1, #8 */
  155. 0xff, 0x20, 0x02, 0xe2, /* and r2, r2, #0xff */
  156. 0x04, 0x20, 0x80, 0xe5, /* str r2, [r0, #0x04] */
  157. /* ; Clear Divisor Latch Access Bit */
  158. /* ; UART_BASE[LCR] &= ~DLAB */
  159. 0x0c, 0x10, 0x90, 0xe5, /* ldr r1, [r0, #0x0c] */
  160. 0x80, 0x10, 0xc1, 0xe3, /* bic r1, r1, #0x80 */
  161. 0x0c, 0x10, 0x80, 0xe5, /* str r1, [r0, #0x0c] */
  162. /* ; Loop 0x2dc000 (2998272) cycles */
  163. /* ; which is about 5ms on 1200 MHz CPU */
  164. /* ; r1 = 0x2dc000 */
  165. 0xb7, 0x19, 0xa0, 0xe3, /* mov r1, #0x2dc000 */
  166. /* .Lloop_sleep: */
  167. 0x01, 0x10, 0x41, 0xe2, /* sub r1, r1, #1 */
  168. 0x00, 0x00, 0x51, 0xe3, /* cmp r1, #0 */
  169. 0xfc, 0xff, 0xff, 0x1a, /* bne .Lloop_sleep */
  170. /* ; Jump to the end of execution */
  171. 0x01, 0x00, 0x00, 0xea, /* b end */
  172. /* ; Placeholder for old baudrate value */
  173. /* old_baudrate: */
  174. 0x00, 0x00, 0x00, 0x00, /* .word 0 */
  175. /* ; Placeholder for new baudrate value */
  176. /* new_baudrate: */
  177. 0x00, 0x00, 0x00, 0x00, /* .word 0 */
  178. /* end: */
  179. };
  180. /* ARM code from binary header executed by BootROM before changing baudrate */
  181. static unsigned char kwboot_baud_code_binhdr_pre[] = {
  182. /* ; #define UART_BASE 0xd0012000 */
  183. /* ; #define THR 0x00 */
  184. /* ; #define LSR 0x14 */
  185. /* ; #define THRE 0x20 */
  186. /* ; */
  187. /* ; void send_preamble(void) { */
  188. /* ; const u8 *str = "$baudratechange"; */
  189. /* ; u8 c; */
  190. /* ; do { */
  191. /* ; while */
  192. /* ; ((readl(UART_BASE + LSR) & THRE)); */
  193. /* ; c = *str++; */
  194. /* ; writel(UART_BASE + THR, c); */
  195. /* ; } while (c); */
  196. /* ; } */
  197. /* ; Preserve registers for BootROM */
  198. 0xfe, 0x5f, 0x2d, 0xe9, /* push { r1 - r12, lr } */
  199. /* ; r0 = UART_BASE */
  200. 0x0d, 0x02, 0xa0, 0xe3, /* mov r0, #0xd0000000 */
  201. 0x12, 0x0a, 0x80, 0xe3, /* orr r0, r0, #0x12000 */
  202. /* ; r2 = address of preamble string */
  203. 0x00, 0x20, 0x8f, 0xe2, /* adr r2, .Lstr_preamble */
  204. /* ; Skip preamble data section */
  205. 0x03, 0x00, 0x00, 0xea, /* b .Lloop_preamble */
  206. /* ; Preamble string */
  207. /* .Lstr_preamble: */
  208. 0x24, 0x62, 0x61, 0x75, /* .asciz "$baudratechange" */
  209. 0x64, 0x72, 0x61, 0x74,
  210. 0x65, 0x63, 0x68, 0x61,
  211. 0x6e, 0x67, 0x65, 0x00,
  212. /* ; Send preamble string over UART */
  213. /* .Lloop_preamble: */
  214. /* */
  215. /* ; Wait until Transmitter Holding is Empty */
  216. /* .Lloop_thre: */
  217. /* ; r1 = UART_BASE[LSR] & THRE */
  218. 0x14, 0x10, 0x90, 0xe5, /* ldr r1, [r0, #0x14] */
  219. 0x20, 0x00, 0x11, 0xe3, /* tst r1, #0x20 */
  220. 0xfc, 0xff, 0xff, 0x0a, /* beq .Lloop_thre */
  221. /* ; Put character into Transmitter FIFO */
  222. /* ; r1 = *r2++ */
  223. 0x01, 0x10, 0xd2, 0xe4, /* ldrb r1, [r2], #1 */
  224. /* ; UART_BASE[THR] = r1 */
  225. 0x00, 0x10, 0x80, 0xe5, /* str r1, [r0, #0x0] */
  226. /* ; Loop until end of preamble string */
  227. 0x00, 0x00, 0x51, 0xe3, /* cmp r1, #0 */
  228. 0xf8, 0xff, 0xff, 0x1a, /* bne .Lloop_preamble */
  229. };
  230. /* ARM code for returning from binary header back to BootROM */
  231. static unsigned char kwboot_baud_code_binhdr_post[] = {
  232. /* ; Return 0 - no error */
  233. 0x00, 0x00, 0xa0, 0xe3, /* mov r0, #0 */
  234. 0xfe, 0x9f, 0xbd, 0xe8, /* pop { r1 - r12, pc } */
  235. };
  236. /* ARM code for jumping to the original image exec_addr */
  237. static unsigned char kwboot_baud_code_data_jump[] = {
  238. 0x04, 0xf0, 0x1f, 0xe5, /* ldr pc, exec_addr */
  239. /* ; Placeholder for exec_addr */
  240. /* exec_addr: */
  241. 0x00, 0x00, 0x00, 0x00, /* .word 0 */
  242. };
  243. static const char kwb_baud_magic[16] = "$baudratechange";
  244. static int kwboot_verbose;
  245. static int msg_req_delay = KWBOOT_MSG_REQ_DELAY;
  246. static int msg_rsp_timeo = KWBOOT_MSG_RSP_TIMEO;
  247. static int blk_rsp_timeo = KWBOOT_BLK_RSP_TIMEO;
  248. static ssize_t
  249. kwboot_write(int fd, const char *buf, size_t len)
  250. {
  251. size_t tot = 0;
  252. while (tot < len) {
  253. ssize_t wr = write(fd, buf + tot, len - tot);
  254. if (wr < 0)
  255. return -1;
  256. tot += wr;
  257. }
  258. return tot;
  259. }
  260. static void
  261. kwboot_printv(const char *fmt, ...)
  262. {
  263. va_list ap;
  264. if (kwboot_verbose) {
  265. va_start(ap, fmt);
  266. vprintf(fmt, ap);
  267. va_end(ap);
  268. fflush(stdout);
  269. }
  270. }
  271. static void
  272. __spinner(void)
  273. {
  274. const char seq[] = { '-', '\\', '|', '/' };
  275. const int div = 8;
  276. static int state, bs;
  277. if (state % div == 0) {
  278. fputc(bs, stdout);
  279. fputc(seq[state / div % sizeof(seq)], stdout);
  280. fflush(stdout);
  281. }
  282. bs = '\b';
  283. state++;
  284. }
  285. static void
  286. kwboot_spinner(void)
  287. {
  288. if (kwboot_verbose)
  289. __spinner();
  290. }
  291. static void
  292. __progress(int pct, char c)
  293. {
  294. const int width = 70;
  295. static const char *nl = "";
  296. static int pos;
  297. if (pos % width == 0)
  298. printf("%s%3d %% [", nl, pct);
  299. fputc(c, stdout);
  300. nl = "]\n";
  301. pos = (pos + 1) % width;
  302. if (pct == 100) {
  303. while (pos && pos++ < width)
  304. fputc(' ', stdout);
  305. fputs(nl, stdout);
  306. nl = "";
  307. pos = 0;
  308. }
  309. fflush(stdout);
  310. }
  311. static void
  312. kwboot_progress(int _pct, char c)
  313. {
  314. static int pct;
  315. if (_pct != -1)
  316. pct = _pct;
  317. if (kwboot_verbose)
  318. __progress(pct, c);
  319. if (pct == 100)
  320. pct = 0;
  321. }
  322. static int
  323. kwboot_tty_recv(int fd, void *buf, size_t len, int timeo)
  324. {
  325. int rc, nfds;
  326. fd_set rfds;
  327. struct timeval tv;
  328. ssize_t n;
  329. rc = -1;
  330. FD_ZERO(&rfds);
  331. FD_SET(fd, &rfds);
  332. tv.tv_sec = 0;
  333. tv.tv_usec = timeo * 1000;
  334. if (tv.tv_usec > 1000000) {
  335. tv.tv_sec += tv.tv_usec / 1000000;
  336. tv.tv_usec %= 1000000;
  337. }
  338. do {
  339. nfds = select(fd + 1, &rfds, NULL, NULL, &tv);
  340. if (nfds < 0)
  341. goto out;
  342. if (!nfds) {
  343. errno = ETIMEDOUT;
  344. goto out;
  345. }
  346. n = read(fd, buf, len);
  347. if (n <= 0)
  348. goto out;
  349. buf = (char *)buf + n;
  350. len -= n;
  351. } while (len > 0);
  352. rc = 0;
  353. out:
  354. return rc;
  355. }
  356. static int
  357. kwboot_tty_send(int fd, const void *buf, size_t len, int nodrain)
  358. {
  359. if (!buf)
  360. return 0;
  361. if (kwboot_write(fd, buf, len) < 0)
  362. return -1;
  363. if (nodrain)
  364. return 0;
  365. return tcdrain(fd);
  366. }
  367. static int
  368. kwboot_tty_send_char(int fd, unsigned char c)
  369. {
  370. return kwboot_tty_send(fd, &c, 1, 0);
  371. }
  372. static speed_t
  373. kwboot_tty_baudrate_to_speed(int baudrate)
  374. {
  375. switch (baudrate) {
  376. #ifdef B4000000
  377. case 4000000:
  378. return B4000000;
  379. #endif
  380. #ifdef B3500000
  381. case 3500000:
  382. return B3500000;
  383. #endif
  384. #ifdef B3000000
  385. case 3000000:
  386. return B3000000;
  387. #endif
  388. #ifdef B2500000
  389. case 2500000:
  390. return B2500000;
  391. #endif
  392. #ifdef B2000000
  393. case 2000000:
  394. return B2000000;
  395. #endif
  396. #ifdef B1500000
  397. case 1500000:
  398. return B1500000;
  399. #endif
  400. #ifdef B1152000
  401. case 1152000:
  402. return B1152000;
  403. #endif
  404. #ifdef B1000000
  405. case 1000000:
  406. return B1000000;
  407. #endif
  408. #ifdef B921600
  409. case 921600:
  410. return B921600;
  411. #endif
  412. #ifdef B614400
  413. case 614400:
  414. return B614400;
  415. #endif
  416. #ifdef B576000
  417. case 576000:
  418. return B576000;
  419. #endif
  420. #ifdef B500000
  421. case 500000:
  422. return B500000;
  423. #endif
  424. #ifdef B460800
  425. case 460800:
  426. return B460800;
  427. #endif
  428. #ifdef B307200
  429. case 307200:
  430. return B307200;
  431. #endif
  432. #ifdef B230400
  433. case 230400:
  434. return B230400;
  435. #endif
  436. #ifdef B153600
  437. case 153600:
  438. return B153600;
  439. #endif
  440. #ifdef B115200
  441. case 115200:
  442. return B115200;
  443. #endif
  444. #ifdef B76800
  445. case 76800:
  446. return B76800;
  447. #endif
  448. #ifdef B57600
  449. case 57600:
  450. return B57600;
  451. #endif
  452. #ifdef B38400
  453. case 38400:
  454. return B38400;
  455. #endif
  456. #ifdef B19200
  457. case 19200:
  458. return B19200;
  459. #endif
  460. #ifdef B9600
  461. case 9600:
  462. return B9600;
  463. #endif
  464. #ifdef B4800
  465. case 4800:
  466. return B4800;
  467. #endif
  468. #ifdef B2400
  469. case 2400:
  470. return B2400;
  471. #endif
  472. #ifdef B1800
  473. case 1800:
  474. return B1800;
  475. #endif
  476. #ifdef B1200
  477. case 1200:
  478. return B1200;
  479. #endif
  480. #ifdef B600
  481. case 600:
  482. return B600;
  483. #endif
  484. #ifdef B300
  485. case 300:
  486. return B300;
  487. #endif
  488. #ifdef B200
  489. case 200:
  490. return B200;
  491. #endif
  492. #ifdef B150
  493. case 150:
  494. return B150;
  495. #endif
  496. #ifdef B134
  497. case 134:
  498. return B134;
  499. #endif
  500. #ifdef B110
  501. case 110:
  502. return B110;
  503. #endif
  504. #ifdef B75
  505. case 75:
  506. return B75;
  507. #endif
  508. #ifdef B50
  509. case 50:
  510. return B50;
  511. #endif
  512. default:
  513. #ifdef BOTHER
  514. return BOTHER;
  515. #else
  516. return B0;
  517. #endif
  518. }
  519. }
  520. static int
  521. _is_within_tolerance(int value, int reference, int tolerance)
  522. {
  523. return 100 * value >= reference * (100 - tolerance) &&
  524. 100 * value <= reference * (100 + tolerance);
  525. }
  526. static int
  527. kwboot_tty_change_baudrate(int fd, int baudrate)
  528. {
  529. struct termios tio;
  530. speed_t speed;
  531. int rc;
  532. rc = tcgetattr(fd, &tio);
  533. if (rc)
  534. return rc;
  535. speed = kwboot_tty_baudrate_to_speed(baudrate);
  536. if (speed == B0) {
  537. errno = EINVAL;
  538. return -1;
  539. }
  540. #ifdef BOTHER
  541. if (speed == BOTHER)
  542. tio.c_ospeed = tio.c_ispeed = baudrate;
  543. #endif
  544. rc = cfsetospeed(&tio, speed);
  545. if (rc)
  546. return rc;
  547. rc = cfsetispeed(&tio, speed);
  548. if (rc)
  549. return rc;
  550. rc = tcsetattr(fd, TCSANOW, &tio);
  551. if (rc)
  552. return rc;
  553. rc = tcgetattr(fd, &tio);
  554. if (rc)
  555. return rc;
  556. if (cfgetospeed(&tio) != speed || cfgetispeed(&tio) != speed)
  557. goto baud_fail;
  558. #ifdef BOTHER
  559. /*
  560. * Check whether set baudrate is within 3% tolerance.
  561. * If BOTHER is defined, Linux always fills out c_ospeed / c_ispeed
  562. * with real values.
  563. */
  564. if (!_is_within_tolerance(tio.c_ospeed, baudrate, 3))
  565. goto baud_fail;
  566. if (!_is_within_tolerance(tio.c_ispeed, baudrate, 3))
  567. goto baud_fail;
  568. #endif
  569. return 0;
  570. baud_fail:
  571. fprintf(stderr, "Could not set baudrate to requested value\n");
  572. errno = EINVAL;
  573. return -1;
  574. }
  575. static int
  576. kwboot_open_tty(const char *path, int baudrate)
  577. {
  578. int rc, fd, flags;
  579. struct termios tio;
  580. rc = -1;
  581. fd = open(path, O_RDWR | O_NOCTTY | O_NDELAY);
  582. if (fd < 0)
  583. goto out;
  584. rc = tcgetattr(fd, &tio);
  585. if (rc)
  586. goto out;
  587. cfmakeraw(&tio);
  588. tio.c_cflag |= CREAD | CLOCAL;
  589. tio.c_cflag &= ~(CSTOPB | HUPCL | CRTSCTS);
  590. tio.c_cc[VMIN] = 1;
  591. tio.c_cc[VTIME] = 0;
  592. rc = tcsetattr(fd, TCSANOW, &tio);
  593. if (rc)
  594. goto out;
  595. flags = fcntl(fd, F_GETFL);
  596. if (flags < 0)
  597. goto out;
  598. rc = fcntl(fd, F_SETFL, flags & ~O_NDELAY);
  599. if (rc)
  600. goto out;
  601. rc = kwboot_tty_change_baudrate(fd, baudrate);
  602. if (rc)
  603. goto out;
  604. rc = fd;
  605. out:
  606. if (rc < 0) {
  607. if (fd >= 0)
  608. close(fd);
  609. }
  610. return rc;
  611. }
  612. static int
  613. kwboot_bootmsg(int tty, void *msg)
  614. {
  615. int rc;
  616. char c;
  617. int count;
  618. if (msg == NULL)
  619. kwboot_printv("Please reboot the target into UART boot mode...");
  620. else
  621. kwboot_printv("Sending boot message. Please reboot the target...");
  622. do {
  623. rc = tcflush(tty, TCIOFLUSH);
  624. if (rc)
  625. break;
  626. for (count = 0; count < 128; count++) {
  627. rc = kwboot_tty_send(tty, msg, 8, 0);
  628. if (rc) {
  629. usleep(msg_req_delay * 1000);
  630. continue;
  631. }
  632. }
  633. rc = kwboot_tty_recv(tty, &c, 1, msg_rsp_timeo);
  634. kwboot_spinner();
  635. } while (rc || c != NAK);
  636. kwboot_printv("\n");
  637. return rc;
  638. }
  639. static int
  640. kwboot_debugmsg(int tty, void *msg)
  641. {
  642. int rc;
  643. kwboot_printv("Sending debug message. Please reboot the target...");
  644. do {
  645. char buf[16];
  646. rc = tcflush(tty, TCIOFLUSH);
  647. if (rc)
  648. break;
  649. rc = kwboot_tty_send(tty, msg, 8, 0);
  650. if (rc) {
  651. usleep(msg_req_delay * 1000);
  652. continue;
  653. }
  654. rc = kwboot_tty_recv(tty, buf, 16, msg_rsp_timeo);
  655. kwboot_spinner();
  656. } while (rc);
  657. kwboot_printv("\n");
  658. return rc;
  659. }
  660. static size_t
  661. kwboot_xm_makeblock(struct kwboot_block *block, const void *data,
  662. size_t size, int pnum)
  663. {
  664. size_t i, n;
  665. block->soh = SOH;
  666. block->pnum = pnum;
  667. block->_pnum = ~block->pnum;
  668. n = size < KWBOOT_XM_BLKSZ ? size : KWBOOT_XM_BLKSZ;
  669. memcpy(&block->data[0], data, n);
  670. memset(&block->data[n], 0, KWBOOT_XM_BLKSZ - n);
  671. block->csum = 0;
  672. for (i = 0; i < n; i++)
  673. block->csum += block->data[i];
  674. return n;
  675. }
  676. static uint64_t
  677. _now(void)
  678. {
  679. struct timespec ts;
  680. if (clock_gettime(CLOCK_MONOTONIC, &ts)) {
  681. static int err_print;
  682. if (!err_print) {
  683. perror("clock_gettime() does not work");
  684. err_print = 1;
  685. }
  686. /* this will just make the timeout not work */
  687. return -1ULL;
  688. }
  689. return ts.tv_sec * 1000ULL + (ts.tv_nsec + 500000) / 1000000;
  690. }
  691. static int
  692. _is_xm_reply(char c)
  693. {
  694. return c == ACK || c == NAK || c == CAN;
  695. }
  696. static int
  697. _xm_reply_to_error(int c)
  698. {
  699. int rc = -1;
  700. switch (c) {
  701. case ACK:
  702. rc = 0;
  703. break;
  704. case NAK:
  705. errno = EBADMSG;
  706. break;
  707. case CAN:
  708. errno = ECANCELED;
  709. break;
  710. default:
  711. errno = EPROTO;
  712. break;
  713. }
  714. return rc;
  715. }
  716. static int
  717. kwboot_baud_magic_handle(int fd, char c, int baudrate)
  718. {
  719. static size_t rcv_len;
  720. if (rcv_len < sizeof(kwb_baud_magic)) {
  721. /* try to recognize whole magic word */
  722. if (c == kwb_baud_magic[rcv_len]) {
  723. rcv_len++;
  724. } else {
  725. printf("%.*s%c", (int)rcv_len, kwb_baud_magic, c);
  726. fflush(stdout);
  727. rcv_len = 0;
  728. }
  729. }
  730. if (rcv_len == sizeof(kwb_baud_magic)) {
  731. /* magic word received */
  732. kwboot_printv("\nChanging baudrate to %d Bd\n", baudrate);
  733. return kwboot_tty_change_baudrate(fd, baudrate) ? : 1;
  734. } else {
  735. return 0;
  736. }
  737. }
  738. static int
  739. kwboot_xm_recv_reply(int fd, char *c, int nak_on_non_xm,
  740. int allow_non_xm, int *non_xm_print,
  741. int baudrate, int *baud_changed)
  742. {
  743. int timeout = allow_non_xm ? KWBOOT_HDR_RSP_TIMEO : blk_rsp_timeo;
  744. uint64_t recv_until = _now() + timeout;
  745. int rc;
  746. while (1) {
  747. rc = kwboot_tty_recv(fd, c, 1, timeout);
  748. if (rc) {
  749. if (errno != ETIMEDOUT)
  750. return rc;
  751. else if (allow_non_xm && *non_xm_print)
  752. return -1;
  753. else
  754. *c = NAK;
  755. }
  756. /* If received xmodem reply, end. */
  757. if (_is_xm_reply(*c))
  758. break;
  759. /*
  760. * If receiving/printing non-xmodem text output is allowed and
  761. * such a byte was received, we want to increase receiving time
  762. * and either:
  763. * - print the byte, if it is not part of baudrate change magic
  764. * sequence while baudrate change was requested (-B option)
  765. * - change baudrate
  766. * Otherwise decrease timeout by time elapsed.
  767. */
  768. if (allow_non_xm) {
  769. recv_until = _now() + timeout;
  770. if (baudrate && !*baud_changed) {
  771. rc = kwboot_baud_magic_handle(fd, *c, baudrate);
  772. if (rc == 1)
  773. *baud_changed = 1;
  774. else if (!rc)
  775. *non_xm_print = 1;
  776. else
  777. return rc;
  778. } else if (!baudrate || !*baud_changed) {
  779. putchar(*c);
  780. fflush(stdout);
  781. *non_xm_print = 1;
  782. }
  783. } else {
  784. if (nak_on_non_xm) {
  785. *c = NAK;
  786. break;
  787. }
  788. timeout = recv_until - _now();
  789. if (timeout < 0) {
  790. errno = ETIMEDOUT;
  791. return -1;
  792. }
  793. }
  794. }
  795. return 0;
  796. }
  797. static int
  798. kwboot_xm_sendblock(int fd, struct kwboot_block *block, int allow_non_xm,
  799. int *done_print, int baudrate)
  800. {
  801. int non_xm_print, baud_changed;
  802. int rc, err, retries;
  803. char c;
  804. *done_print = 0;
  805. non_xm_print = 0;
  806. baud_changed = 0;
  807. retries = 0;
  808. do {
  809. rc = kwboot_tty_send(fd, block, sizeof(*block), 1);
  810. if (rc)
  811. return rc;
  812. if (allow_non_xm && !*done_print) {
  813. kwboot_progress(100, '.');
  814. kwboot_printv("Done\n");
  815. *done_print = 1;
  816. }
  817. rc = kwboot_xm_recv_reply(fd, &c, retries < 3,
  818. allow_non_xm, &non_xm_print,
  819. baudrate, &baud_changed);
  820. if (rc)
  821. goto can;
  822. if (!allow_non_xm && c != ACK)
  823. kwboot_progress(-1, '+');
  824. } while (c == NAK && retries++ < 16);
  825. if (non_xm_print)
  826. kwboot_printv("\n");
  827. if (allow_non_xm && baudrate && !baud_changed) {
  828. fprintf(stderr, "Baudrate was not changed\n");
  829. rc = -1;
  830. errno = EPROTO;
  831. goto can;
  832. }
  833. return _xm_reply_to_error(c);
  834. can:
  835. err = errno;
  836. kwboot_tty_send_char(fd, CAN);
  837. kwboot_printv("\n");
  838. errno = err;
  839. return rc;
  840. }
  841. static int
  842. kwboot_xm_finish(int fd)
  843. {
  844. int rc, retries;
  845. char c;
  846. kwboot_printv("Finishing transfer\n");
  847. retries = 0;
  848. do {
  849. rc = kwboot_tty_send_char(fd, EOT);
  850. if (rc)
  851. return rc;
  852. rc = kwboot_xm_recv_reply(fd, &c, retries < 3,
  853. 0, NULL, 0, NULL);
  854. if (rc)
  855. return rc;
  856. } while (c == NAK && retries++ < 16);
  857. return _xm_reply_to_error(c);
  858. }
  859. static int
  860. kwboot_xmodem_one(int tty, int *pnum, int header, const uint8_t *data,
  861. size_t size, int baudrate)
  862. {
  863. int done_print = 0;
  864. size_t sent, left;
  865. int rc;
  866. kwboot_printv("Sending boot image %s (%zu bytes)...\n",
  867. header ? "header" : "data", size);
  868. left = size;
  869. sent = 0;
  870. while (sent < size) {
  871. struct kwboot_block block;
  872. int last_block;
  873. size_t blksz;
  874. blksz = kwboot_xm_makeblock(&block, data, left, (*pnum)++);
  875. data += blksz;
  876. last_block = (left <= blksz);
  877. rc = kwboot_xm_sendblock(tty, &block, header && last_block,
  878. &done_print, baudrate);
  879. if (rc)
  880. goto out;
  881. sent += blksz;
  882. left -= blksz;
  883. if (!done_print)
  884. kwboot_progress(sent * 100 / size, '.');
  885. }
  886. if (!done_print)
  887. kwboot_printv("Done\n");
  888. return 0;
  889. out:
  890. kwboot_printv("\n");
  891. return rc;
  892. }
  893. static int
  894. kwboot_xmodem(int tty, const void *_img, size_t size, int baudrate)
  895. {
  896. const uint8_t *img = _img;
  897. int rc, pnum;
  898. size_t hdrsz;
  899. hdrsz = kwbheader_size(img);
  900. /*
  901. * If header size is not aligned to xmodem block size (which applies
  902. * for all images in kwbimage v0 format) then we have to ensure that
  903. * the last xmodem block of header contains beginning of the data
  904. * followed by the header. So align header size to xmodem block size.
  905. */
  906. hdrsz += (KWBOOT_XM_BLKSZ - hdrsz % KWBOOT_XM_BLKSZ) % KWBOOT_XM_BLKSZ;
  907. kwboot_printv("Waiting 2s and flushing tty\n");
  908. sleep(2); /* flush isn't effective without it */
  909. tcflush(tty, TCIOFLUSH);
  910. pnum = 1;
  911. rc = kwboot_xmodem_one(tty, &pnum, 1, img, hdrsz, baudrate);
  912. if (rc)
  913. return rc;
  914. /*
  915. * If we have already sent image data as a part of the last
  916. * xmodem header block then we have nothing more to send.
  917. */
  918. if (hdrsz < size) {
  919. img += hdrsz;
  920. size -= hdrsz;
  921. rc = kwboot_xmodem_one(tty, &pnum, 0, img, size, 0);
  922. if (rc)
  923. return rc;
  924. }
  925. rc = kwboot_xm_finish(tty);
  926. if (rc)
  927. return rc;
  928. if (baudrate) {
  929. kwboot_printv("\nChanging baudrate back to 115200 Bd\n\n");
  930. rc = kwboot_tty_change_baudrate(tty, 115200);
  931. if (rc)
  932. return rc;
  933. }
  934. return 0;
  935. }
  936. static int
  937. kwboot_term_pipe(int in, int out, const char *quit, int *s)
  938. {
  939. ssize_t nin;
  940. char _buf[128], *buf = _buf;
  941. nin = read(in, buf, sizeof(_buf));
  942. if (nin <= 0)
  943. return -1;
  944. if (quit) {
  945. int i;
  946. for (i = 0; i < nin; i++) {
  947. if (*buf == quit[*s]) {
  948. (*s)++;
  949. if (!quit[*s])
  950. return 0;
  951. buf++;
  952. nin--;
  953. } else {
  954. if (kwboot_write(out, quit, *s) < 0)
  955. return -1;
  956. *s = 0;
  957. }
  958. }
  959. }
  960. if (kwboot_write(out, buf, nin) < 0)
  961. return -1;
  962. return 0;
  963. }
  964. static int
  965. kwboot_terminal(int tty)
  966. {
  967. int rc, in, s;
  968. const char *quit = "\34c";
  969. struct termios otio, tio;
  970. rc = -1;
  971. in = STDIN_FILENO;
  972. if (isatty(in)) {
  973. rc = tcgetattr(in, &otio);
  974. if (!rc) {
  975. tio = otio;
  976. cfmakeraw(&tio);
  977. rc = tcsetattr(in, TCSANOW, &tio);
  978. }
  979. if (rc) {
  980. perror("tcsetattr");
  981. goto out;
  982. }
  983. kwboot_printv("[Type Ctrl-%c + %c to quit]\r\n",
  984. quit[0] | 0100, quit[1]);
  985. } else
  986. in = -1;
  987. rc = 0;
  988. s = 0;
  989. do {
  990. fd_set rfds;
  991. int nfds = 0;
  992. FD_ZERO(&rfds);
  993. FD_SET(tty, &rfds);
  994. nfds = nfds < tty ? tty : nfds;
  995. if (in >= 0) {
  996. FD_SET(in, &rfds);
  997. nfds = nfds < in ? in : nfds;
  998. }
  999. nfds = select(nfds + 1, &rfds, NULL, NULL, NULL);
  1000. if (nfds < 0)
  1001. break;
  1002. if (FD_ISSET(tty, &rfds)) {
  1003. rc = kwboot_term_pipe(tty, STDOUT_FILENO, NULL, NULL);
  1004. if (rc)
  1005. break;
  1006. }
  1007. if (in >= 0 && FD_ISSET(in, &rfds)) {
  1008. rc = kwboot_term_pipe(in, tty, quit, &s);
  1009. if (rc)
  1010. break;
  1011. }
  1012. } while (quit[s] != 0);
  1013. if (in >= 0)
  1014. tcsetattr(in, TCSANOW, &otio);
  1015. printf("\n");
  1016. out:
  1017. return rc;
  1018. }
  1019. static void *
  1020. kwboot_read_image(const char *path, size_t *size, size_t reserve)
  1021. {
  1022. int rc, fd;
  1023. struct stat st;
  1024. void *img;
  1025. off_t tot;
  1026. rc = -1;
  1027. img = NULL;
  1028. fd = open(path, O_RDONLY);
  1029. if (fd < 0)
  1030. goto out;
  1031. rc = fstat(fd, &st);
  1032. if (rc)
  1033. goto out;
  1034. img = malloc(st.st_size + reserve);
  1035. if (!img)
  1036. goto out;
  1037. tot = 0;
  1038. while (tot < st.st_size) {
  1039. ssize_t rd = read(fd, img + tot, st.st_size - tot);
  1040. if (rd < 0)
  1041. goto out;
  1042. tot += rd;
  1043. if (!rd && tot < st.st_size) {
  1044. errno = EIO;
  1045. goto out;
  1046. }
  1047. }
  1048. rc = 0;
  1049. *size = st.st_size;
  1050. out:
  1051. if (rc && img) {
  1052. free(img);
  1053. img = NULL;
  1054. }
  1055. if (fd >= 0)
  1056. close(fd);
  1057. return img;
  1058. }
  1059. static uint8_t
  1060. kwboot_hdr_csum8(const void *hdr)
  1061. {
  1062. const uint8_t *data = hdr;
  1063. uint8_t csum;
  1064. size_t size;
  1065. size = kwbheader_size_for_csum(hdr);
  1066. for (csum = 0; size-- > 0; data++)
  1067. csum += *data;
  1068. return csum;
  1069. }
  1070. static uint32_t *
  1071. kwboot_img_csum32_ptr(void *img)
  1072. {
  1073. struct main_hdr_v1 *hdr = img;
  1074. uint32_t datasz;
  1075. datasz = le32_to_cpu(hdr->blocksize) - sizeof(uint32_t);
  1076. return img + le32_to_cpu(hdr->srcaddr) + datasz;
  1077. }
  1078. static uint32_t
  1079. kwboot_img_csum32(const void *img)
  1080. {
  1081. const struct main_hdr_v1 *hdr = img;
  1082. uint32_t datasz, csum = 0;
  1083. const uint32_t *data;
  1084. datasz = le32_to_cpu(hdr->blocksize) - sizeof(csum);
  1085. if (datasz % sizeof(uint32_t))
  1086. return 0;
  1087. data = img + le32_to_cpu(hdr->srcaddr);
  1088. while (datasz > 0) {
  1089. csum += le32_to_cpu(*data++);
  1090. datasz -= 4;
  1091. }
  1092. return cpu_to_le32(csum);
  1093. }
  1094. static int
  1095. kwboot_img_is_secure(void *img)
  1096. {
  1097. struct opt_hdr_v1 *ohdr;
  1098. for_each_opt_hdr_v1 (ohdr, img)
  1099. if (ohdr->headertype == OPT_HDR_V1_SECURE_TYPE)
  1100. return 1;
  1101. return 0;
  1102. }
  1103. static void *
  1104. kwboot_img_grow_data_right(void *img, size_t *size, size_t grow)
  1105. {
  1106. struct main_hdr_v1 *hdr = img;
  1107. void *result;
  1108. /*
  1109. * 32-bit checksum comes after end of image code, so we will be putting
  1110. * new code there. So we get this pointer and then increase data size
  1111. * (since increasing data size changes kwboot_img_csum32_ptr() return
  1112. * value).
  1113. */
  1114. result = kwboot_img_csum32_ptr(img);
  1115. hdr->blocksize = cpu_to_le32(le32_to_cpu(hdr->blocksize) + grow);
  1116. *size += grow;
  1117. return result;
  1118. }
  1119. static void
  1120. kwboot_img_grow_hdr(void *img, size_t *size, size_t grow)
  1121. {
  1122. uint32_t hdrsz, datasz, srcaddr;
  1123. struct main_hdr_v1 *hdr = img;
  1124. struct opt_hdr_v1 *ohdr;
  1125. uint8_t *data;
  1126. srcaddr = le32_to_cpu(hdr->srcaddr);
  1127. /* calculate real used space in kwbimage header */
  1128. if (kwbimage_version(img) == 0) {
  1129. hdrsz = kwbheader_size(img);
  1130. } else {
  1131. hdrsz = sizeof(*hdr);
  1132. for_each_opt_hdr_v1 (ohdr, hdr)
  1133. hdrsz += opt_hdr_v1_size(ohdr);
  1134. }
  1135. data = (uint8_t *)img + srcaddr;
  1136. datasz = *size - srcaddr;
  1137. /* only move data if there is not enough space */
  1138. if (hdrsz + grow > srcaddr) {
  1139. size_t need = hdrsz + grow - srcaddr;
  1140. /* move data by enough bytes */
  1141. memmove(data + need, data, datasz);
  1142. hdr->srcaddr = cpu_to_le32(srcaddr + need);
  1143. *size += need;
  1144. }
  1145. if (kwbimage_version(img) == 1) {
  1146. hdrsz += grow;
  1147. if (hdrsz > kwbheader_size(img)) {
  1148. hdr->headersz_msb = hdrsz >> 16;
  1149. hdr->headersz_lsb = cpu_to_le16(hdrsz & 0xffff);
  1150. }
  1151. }
  1152. }
  1153. static void *
  1154. kwboot_add_bin_ohdr_v1(void *img, size_t *size, uint32_t binsz)
  1155. {
  1156. struct main_hdr_v1 *hdr = img;
  1157. struct opt_hdr_v1 *ohdr;
  1158. uint32_t num_args;
  1159. uint32_t offset;
  1160. uint32_t ohdrsz;
  1161. uint8_t *prev_ext;
  1162. if (hdr->ext & 0x1) {
  1163. for_each_opt_hdr_v1 (ohdr, img)
  1164. if (opt_hdr_v1_next(ohdr) == NULL)
  1165. break;
  1166. prev_ext = opt_hdr_v1_ext(ohdr);
  1167. ohdr = _opt_hdr_v1_next(ohdr);
  1168. } else {
  1169. ohdr = (void *)(hdr + 1);
  1170. prev_ext = &hdr->ext;
  1171. }
  1172. /*
  1173. * ARM executable code inside the BIN header on some mvebu platforms
  1174. * (e.g. A370, AXP) must always be aligned with the 128-bit boundary.
  1175. * This requirement can be met by inserting dummy arguments into
  1176. * BIN header, if needed.
  1177. */
  1178. offset = &ohdr->data[4] - (char *)img;
  1179. num_args = ((16 - offset % 16) % 16) / sizeof(uint32_t);
  1180. ohdrsz = sizeof(*ohdr) + 4 + 4 * num_args + binsz + 4;
  1181. kwboot_img_grow_hdr(hdr, size, ohdrsz);
  1182. *prev_ext |= 1;
  1183. ohdr->headertype = OPT_HDR_V1_BINARY_TYPE;
  1184. ohdr->headersz_msb = ohdrsz >> 16;
  1185. ohdr->headersz_lsb = cpu_to_le16(ohdrsz & 0xffff);
  1186. memset(&ohdr->data[0], 0, ohdrsz - sizeof(*ohdr));
  1187. *(uint32_t *)&ohdr->data[0] = cpu_to_le32(num_args);
  1188. return &ohdr->data[4 + 4 * num_args];
  1189. }
  1190. static void
  1191. _inject_baudrate_change_code(void *img, size_t *size, int for_data,
  1192. int old_baud, int new_baud)
  1193. {
  1194. struct main_hdr_v1 *hdr = img;
  1195. uint32_t orig_datasz;
  1196. uint32_t codesz;
  1197. uint8_t *code;
  1198. if (for_data) {
  1199. orig_datasz = le32_to_cpu(hdr->blocksize) - sizeof(uint32_t);
  1200. codesz = sizeof(kwboot_baud_code) +
  1201. sizeof(kwboot_baud_code_data_jump);
  1202. code = kwboot_img_grow_data_right(img, size, codesz);
  1203. } else {
  1204. codesz = sizeof(kwboot_baud_code_binhdr_pre) +
  1205. sizeof(kwboot_baud_code) +
  1206. sizeof(kwboot_baud_code_binhdr_post);
  1207. code = kwboot_add_bin_ohdr_v1(img, size, codesz);
  1208. codesz = sizeof(kwboot_baud_code_binhdr_pre);
  1209. memcpy(code, kwboot_baud_code_binhdr_pre, codesz);
  1210. code += codesz;
  1211. }
  1212. codesz = sizeof(kwboot_baud_code) - 2 * sizeof(uint32_t);
  1213. memcpy(code, kwboot_baud_code, codesz);
  1214. code += codesz;
  1215. *(uint32_t *)code = cpu_to_le32(old_baud);
  1216. code += sizeof(uint32_t);
  1217. *(uint32_t *)code = cpu_to_le32(new_baud);
  1218. code += sizeof(uint32_t);
  1219. if (for_data) {
  1220. codesz = sizeof(kwboot_baud_code_data_jump) - sizeof(uint32_t);
  1221. memcpy(code, kwboot_baud_code_data_jump, codesz);
  1222. code += codesz;
  1223. *(uint32_t *)code = hdr->execaddr;
  1224. code += sizeof(uint32_t);
  1225. hdr->execaddr = cpu_to_le32(le32_to_cpu(hdr->destaddr) + orig_datasz);
  1226. } else {
  1227. codesz = sizeof(kwboot_baud_code_binhdr_post);
  1228. memcpy(code, kwboot_baud_code_binhdr_post, codesz);
  1229. code += codesz;
  1230. }
  1231. }
  1232. static int
  1233. kwboot_img_patch(void *img, size_t *size, int baudrate)
  1234. {
  1235. struct main_hdr_v1 *hdr;
  1236. uint32_t srcaddr;
  1237. uint8_t csum;
  1238. size_t hdrsz;
  1239. int image_ver;
  1240. int is_secure;
  1241. hdr = img;
  1242. if (*size < sizeof(struct main_hdr_v1))
  1243. goto err;
  1244. image_ver = kwbimage_version(img);
  1245. if (image_ver != 0 && image_ver != 1) {
  1246. fprintf(stderr, "Invalid image header version\n");
  1247. goto err;
  1248. }
  1249. hdrsz = kwbheader_size(hdr);
  1250. if (*size < hdrsz)
  1251. goto err;
  1252. csum = kwboot_hdr_csum8(hdr) - hdr->checksum;
  1253. if (csum != hdr->checksum)
  1254. goto err;
  1255. srcaddr = le32_to_cpu(hdr->srcaddr);
  1256. switch (hdr->blockid) {
  1257. case IBR_HDR_SATA_ID:
  1258. if (srcaddr < 1)
  1259. goto err;
  1260. hdr->srcaddr = cpu_to_le32((srcaddr - 1) * 512);
  1261. break;
  1262. case IBR_HDR_SDIO_ID:
  1263. hdr->srcaddr = cpu_to_le32(srcaddr * 512);
  1264. break;
  1265. case IBR_HDR_PEX_ID:
  1266. if (srcaddr == 0xFFFFFFFF)
  1267. hdr->srcaddr = cpu_to_le32(hdrsz);
  1268. break;
  1269. case IBR_HDR_SPI_ID:
  1270. if (hdr->destaddr == cpu_to_le32(0xFFFFFFFF)) {
  1271. kwboot_printv("Patching destination and execution addresses from SPI/NOR XIP area to DDR area 0x00800000\n");
  1272. hdr->destaddr = cpu_to_le32(0x00800000);
  1273. hdr->execaddr = cpu_to_le32(0x00800000);
  1274. }
  1275. break;
  1276. }
  1277. if (hdrsz > le32_to_cpu(hdr->srcaddr) ||
  1278. *size < le32_to_cpu(hdr->srcaddr) + le32_to_cpu(hdr->blocksize))
  1279. goto err;
  1280. if (kwboot_img_csum32(img) != *kwboot_img_csum32_ptr(img))
  1281. goto err;
  1282. is_secure = kwboot_img_is_secure(img);
  1283. if (hdr->blockid != IBR_HDR_UART_ID) {
  1284. if (is_secure) {
  1285. fprintf(stderr,
  1286. "Image has secure header with signature for non-UART booting\n");
  1287. goto err;
  1288. }
  1289. kwboot_printv("Patching image boot signature to UART\n");
  1290. hdr->blockid = IBR_HDR_UART_ID;
  1291. }
  1292. if (!is_secure) {
  1293. if (image_ver == 1) {
  1294. /*
  1295. * Tell BootROM to send BootROM messages to UART port
  1296. * number 0 (used also for UART booting) with default
  1297. * baudrate (which should be 115200) and do not touch
  1298. * UART MPP configuration.
  1299. */
  1300. hdr->options &= ~0x1F;
  1301. hdr->options |= MAIN_HDR_V1_OPT_BAUD_DEFAULT;
  1302. hdr->options |= 0 << 3;
  1303. }
  1304. if (image_ver == 0)
  1305. ((struct main_hdr_v0 *)img)->nandeccmode = IBR_HDR_ECC_DISABLED;
  1306. hdr->nandpagesize = 0;
  1307. }
  1308. if (baudrate) {
  1309. if (image_ver == 0) {
  1310. fprintf(stderr,
  1311. "Cannot inject code for changing baudrate into v0 image header\n");
  1312. goto err;
  1313. }
  1314. if (is_secure) {
  1315. fprintf(stderr,
  1316. "Cannot inject code for changing baudrate into image with secure header\n");
  1317. goto err;
  1318. }
  1319. /*
  1320. * First inject code that changes the baudrate from the default
  1321. * value of 115200 Bd to requested value. This code is inserted
  1322. * as a new opt hdr, so it is executed by BootROM after the
  1323. * header part is received.
  1324. */
  1325. kwboot_printv("Injecting binary header code for changing baudrate to %d Bd\n",
  1326. baudrate);
  1327. _inject_baudrate_change_code(img, size, 0, 115200, baudrate);
  1328. /*
  1329. * Now inject code that changes the baudrate back to 115200 Bd.
  1330. * This code is appended after the data part of the image, and
  1331. * execaddr is changed so that it is executed before U-Boot
  1332. * proper.
  1333. */
  1334. kwboot_printv("Injecting code for changing baudrate back\n");
  1335. _inject_baudrate_change_code(img, size, 1, baudrate, 115200);
  1336. /* Update the 32-bit data checksum */
  1337. *kwboot_img_csum32_ptr(img) = kwboot_img_csum32(img);
  1338. /* recompute header size */
  1339. hdrsz = kwbheader_size(hdr);
  1340. }
  1341. if (hdrsz % KWBOOT_XM_BLKSZ) {
  1342. size_t grow = KWBOOT_XM_BLKSZ - hdrsz % KWBOOT_XM_BLKSZ;
  1343. if (is_secure) {
  1344. fprintf(stderr, "Cannot align image with secure header\n");
  1345. goto err;
  1346. }
  1347. kwboot_printv("Aligning image header to Xmodem block size\n");
  1348. kwboot_img_grow_hdr(img, size, grow);
  1349. }
  1350. hdr->checksum = kwboot_hdr_csum8(hdr) - csum;
  1351. *size = le32_to_cpu(hdr->srcaddr) + le32_to_cpu(hdr->blocksize);
  1352. return 0;
  1353. err:
  1354. errno = EINVAL;
  1355. return -1;
  1356. }
  1357. static void
  1358. kwboot_usage(FILE *stream, char *progname)
  1359. {
  1360. fprintf(stream,
  1361. "Usage: %s [OPTIONS] [-b <image> | -D <image> ] [-B <baud> ] <TTY>\n",
  1362. progname);
  1363. fprintf(stream, "\n");
  1364. fprintf(stream,
  1365. " -b <image>: boot <image> with preamble (Kirkwood, Armada 370/XP)\n");
  1366. fprintf(stream,
  1367. " -D <image>: boot <image> without preamble (Dove)\n");
  1368. fprintf(stream, " -d: enter debug mode\n");
  1369. fprintf(stream, " -a: use timings for Armada XP\n");
  1370. fprintf(stream, " -q <req-delay>: use specific request-delay\n");
  1371. fprintf(stream, " -s <resp-timeo>: use specific response-timeout\n");
  1372. fprintf(stream,
  1373. " -o <block-timeo>: use specific xmodem block timeout\n");
  1374. fprintf(stream, "\n");
  1375. fprintf(stream, " -t: mini terminal\n");
  1376. fprintf(stream, "\n");
  1377. fprintf(stream, " -B <baud>: set baud rate\n");
  1378. fprintf(stream, "\n");
  1379. }
  1380. int
  1381. main(int argc, char **argv)
  1382. {
  1383. const char *ttypath, *imgpath;
  1384. int rv, rc, tty, term;
  1385. void *bootmsg;
  1386. void *debugmsg;
  1387. void *img;
  1388. size_t size;
  1389. size_t after_img_rsv;
  1390. int baudrate;
  1391. rv = 1;
  1392. tty = -1;
  1393. bootmsg = NULL;
  1394. debugmsg = NULL;
  1395. imgpath = NULL;
  1396. img = NULL;
  1397. term = 0;
  1398. size = 0;
  1399. after_img_rsv = KWBOOT_XM_BLKSZ;
  1400. baudrate = 115200;
  1401. printf("kwboot version %s\n", PLAIN_VERSION);
  1402. kwboot_verbose = isatty(STDOUT_FILENO);
  1403. do {
  1404. int c = getopt(argc, argv, "hb:ptaB:dD:q:s:o:");
  1405. if (c < 0)
  1406. break;
  1407. switch (c) {
  1408. case 'b':
  1409. bootmsg = kwboot_msg_boot;
  1410. imgpath = optarg;
  1411. break;
  1412. case 'D':
  1413. bootmsg = NULL;
  1414. imgpath = optarg;
  1415. break;
  1416. case 'd':
  1417. debugmsg = kwboot_msg_debug;
  1418. break;
  1419. case 'p':
  1420. /* nop, for backward compatibility */
  1421. break;
  1422. case 't':
  1423. term = 1;
  1424. break;
  1425. case 'a':
  1426. msg_req_delay = KWBOOT_MSG_REQ_DELAY_AXP;
  1427. msg_rsp_timeo = KWBOOT_MSG_RSP_TIMEO_AXP;
  1428. break;
  1429. case 'q':
  1430. msg_req_delay = atoi(optarg);
  1431. break;
  1432. case 's':
  1433. msg_rsp_timeo = atoi(optarg);
  1434. break;
  1435. case 'o':
  1436. blk_rsp_timeo = atoi(optarg);
  1437. break;
  1438. case 'B':
  1439. baudrate = atoi(optarg);
  1440. break;
  1441. case 'h':
  1442. rv = 0;
  1443. default:
  1444. goto usage;
  1445. }
  1446. } while (1);
  1447. if (!bootmsg && !term && !debugmsg)
  1448. goto usage;
  1449. if (argc - optind < 1)
  1450. goto usage;
  1451. ttypath = argv[optind++];
  1452. tty = kwboot_open_tty(ttypath, imgpath ? 115200 : baudrate);
  1453. if (tty < 0) {
  1454. perror(ttypath);
  1455. goto out;
  1456. }
  1457. if (baudrate == 115200)
  1458. /* do not change baudrate during Xmodem to the same value */
  1459. baudrate = 0;
  1460. else
  1461. /* ensure we have enough space for baudrate change code */
  1462. after_img_rsv += sizeof(struct opt_hdr_v1) + 8 + 16 +
  1463. sizeof(kwboot_baud_code_binhdr_pre) +
  1464. sizeof(kwboot_baud_code) +
  1465. sizeof(kwboot_baud_code_binhdr_post) +
  1466. KWBOOT_XM_BLKSZ +
  1467. sizeof(kwboot_baud_code) +
  1468. sizeof(kwboot_baud_code_data_jump) +
  1469. KWBOOT_XM_BLKSZ;
  1470. if (imgpath) {
  1471. img = kwboot_read_image(imgpath, &size, after_img_rsv);
  1472. if (!img) {
  1473. perror(imgpath);
  1474. goto out;
  1475. }
  1476. rc = kwboot_img_patch(img, &size, baudrate);
  1477. if (rc) {
  1478. fprintf(stderr, "%s: Invalid image.\n", imgpath);
  1479. goto out;
  1480. }
  1481. }
  1482. if (debugmsg) {
  1483. rc = kwboot_debugmsg(tty, debugmsg);
  1484. if (rc) {
  1485. perror("debugmsg");
  1486. goto out;
  1487. }
  1488. } else if (bootmsg) {
  1489. rc = kwboot_bootmsg(tty, bootmsg);
  1490. if (rc) {
  1491. perror("bootmsg");
  1492. goto out;
  1493. }
  1494. }
  1495. if (img) {
  1496. rc = kwboot_xmodem(tty, img, size, baudrate);
  1497. if (rc) {
  1498. perror("xmodem");
  1499. goto out;
  1500. }
  1501. }
  1502. if (term) {
  1503. rc = kwboot_terminal(tty);
  1504. if (rc && !(errno == EINTR)) {
  1505. perror("terminal");
  1506. goto out;
  1507. }
  1508. }
  1509. rv = 0;
  1510. out:
  1511. if (tty >= 0)
  1512. close(tty);
  1513. if (img)
  1514. free(img);
  1515. return rv;
  1516. usage:
  1517. kwboot_usage(rv ? stderr : stdout, basename(argv[0]));
  1518. goto out;
  1519. }