kwboot.c 43 KB

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