altera.c 55 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * altera.c
  4. *
  5. * altera FPGA driver
  6. *
  7. * Copyright (C) Altera Corporation 1998-2001
  8. * Copyright (C) 2010,2011 NetUP Inc.
  9. * Copyright (C) 2010,2011 Igor M. Liplianin <liplianin@netup.ru>
  10. */
  11. #include <linux/unaligned.h>
  12. #include <linux/ctype.h>
  13. #include <linux/string.h>
  14. #include <linux/firmware.h>
  15. #include <linux/slab.h>
  16. #include <linux/module.h>
  17. #include <misc/altera.h>
  18. #include "altera-exprt.h"
  19. #include "altera-jtag.h"
  20. static int debug = 1;
  21. module_param(debug, int, 0644);
  22. MODULE_PARM_DESC(debug, "enable debugging information");
  23. MODULE_DESCRIPTION("altera FPGA kernel module");
  24. MODULE_AUTHOR("Igor M. Liplianin <liplianin@netup.ru>");
  25. MODULE_LICENSE("GPL");
  26. #define dprintk(args...) \
  27. if (debug) { \
  28. printk(KERN_DEBUG args); \
  29. }
  30. enum altera_fpga_opcode {
  31. OP_NOP = 0,
  32. OP_DUP,
  33. OP_SWP,
  34. OP_ADD,
  35. OP_SUB,
  36. OP_MULT,
  37. OP_DIV,
  38. OP_MOD,
  39. OP_SHL,
  40. OP_SHR,
  41. OP_NOT,
  42. OP_AND,
  43. OP_OR,
  44. OP_XOR,
  45. OP_INV,
  46. OP_GT,
  47. OP_LT,
  48. OP_RET,
  49. OP_CMPS,
  50. OP_PINT,
  51. OP_PRNT,
  52. OP_DSS,
  53. OP_DSSC,
  54. OP_ISS,
  55. OP_ISSC,
  56. OP_DPR = 0x1c,
  57. OP_DPRL,
  58. OP_DPO,
  59. OP_DPOL,
  60. OP_IPR,
  61. OP_IPRL,
  62. OP_IPO,
  63. OP_IPOL,
  64. OP_PCHR,
  65. OP_EXIT,
  66. OP_EQU,
  67. OP_POPT,
  68. OP_ABS = 0x2c,
  69. OP_BCH0,
  70. OP_PSH0 = 0x2f,
  71. OP_PSHL = 0x40,
  72. OP_PSHV,
  73. OP_JMP,
  74. OP_CALL,
  75. OP_NEXT,
  76. OP_PSTR,
  77. OP_SINT = 0x47,
  78. OP_ST,
  79. OP_ISTP,
  80. OP_DSTP,
  81. OP_SWPN,
  82. OP_DUPN,
  83. OP_POPV,
  84. OP_POPE,
  85. OP_POPA,
  86. OP_JMPZ,
  87. OP_DS,
  88. OP_IS,
  89. OP_DPRA,
  90. OP_DPOA,
  91. OP_IPRA,
  92. OP_IPOA,
  93. OP_EXPT,
  94. OP_PSHE,
  95. OP_PSHA,
  96. OP_DYNA,
  97. OP_EXPV = 0x5c,
  98. OP_COPY = 0x80,
  99. OP_REVA,
  100. OP_DSC,
  101. OP_ISC,
  102. OP_WAIT,
  103. OP_VS,
  104. OP_CMPA = 0xc0,
  105. OP_VSC,
  106. };
  107. struct altera_procinfo {
  108. char *name;
  109. u8 attrs;
  110. struct altera_procinfo *next;
  111. };
  112. /* This function checks if enough parameters are available on the stack. */
  113. static int altera_check_stack(int stack_ptr, int count, int *status)
  114. {
  115. if (stack_ptr < count) {
  116. *status = -EOVERFLOW;
  117. return 0;
  118. }
  119. return 1;
  120. }
  121. static void altera_export_int(char *key, s32 value)
  122. {
  123. dprintk("Export: key = \"%s\", value = %d\n", key, value);
  124. }
  125. #define HEX_LINE_CHARS 72
  126. #define HEX_LINE_BITS (HEX_LINE_CHARS * 4)
  127. static void altera_export_bool_array(char *key, u8 *data, s32 count)
  128. {
  129. char string[HEX_LINE_CHARS + 1];
  130. s32 i, offset;
  131. u32 size, line, lines, linebits, value, j, k;
  132. if (count > HEX_LINE_BITS) {
  133. dprintk("Export: key = \"%s\", %d bits, value = HEX\n",
  134. key, count);
  135. lines = (count + (HEX_LINE_BITS - 1)) / HEX_LINE_BITS;
  136. for (line = 0; line < lines; ++line) {
  137. if (line < (lines - 1)) {
  138. linebits = HEX_LINE_BITS;
  139. size = HEX_LINE_CHARS;
  140. offset = count - ((line + 1) * HEX_LINE_BITS);
  141. } else {
  142. linebits =
  143. count - ((lines - 1) * HEX_LINE_BITS);
  144. size = (linebits + 3) / 4;
  145. offset = 0L;
  146. }
  147. string[size] = '\0';
  148. j = size - 1;
  149. value = 0;
  150. for (k = 0; k < linebits; ++k) {
  151. i = k + offset;
  152. if (data[i >> 3] & (1 << (i & 7)))
  153. value |= (1 << (i & 3));
  154. if ((i & 3) == 3) {
  155. sprintf(&string[j], "%1x", value);
  156. value = 0;
  157. --j;
  158. }
  159. }
  160. if ((k & 3) > 0)
  161. sprintf(&string[j], "%1x", value);
  162. dprintk("%s\n", string);
  163. }
  164. } else {
  165. size = (count + 3) / 4;
  166. string[size] = '\0';
  167. j = size - 1;
  168. value = 0;
  169. for (i = 0; i < count; ++i) {
  170. if (data[i >> 3] & (1 << (i & 7)))
  171. value |= (1 << (i & 3));
  172. if ((i & 3) == 3) {
  173. sprintf(&string[j], "%1x", value);
  174. value = 0;
  175. --j;
  176. }
  177. }
  178. if ((i & 3) > 0)
  179. sprintf(&string[j], "%1x", value);
  180. dprintk("Export: key = \"%s\", %d bits, value = HEX %s\n",
  181. key, count, string);
  182. }
  183. }
  184. static int altera_execute(struct altera_state *astate,
  185. u8 *p,
  186. s32 program_size,
  187. s32 *error_address,
  188. int *exit_code,
  189. int *format_version)
  190. {
  191. struct altera_config *aconf = astate->config;
  192. char *msg_buff = astate->msg_buff;
  193. long *stack = astate->stack;
  194. int status = 0;
  195. u32 first_word = 0L;
  196. u32 action_table = 0L;
  197. u32 proc_table = 0L;
  198. u32 str_table = 0L;
  199. u32 sym_table = 0L;
  200. u32 data_sect = 0L;
  201. u32 code_sect = 0L;
  202. u32 debug_sect = 0L;
  203. u32 action_count = 0L;
  204. u32 proc_count = 0L;
  205. u32 sym_count = 0L;
  206. long *vars = NULL;
  207. s32 *var_size = NULL;
  208. char *attrs = NULL;
  209. u8 *proc_attributes = NULL;
  210. u32 pc;
  211. u32 opcode_address;
  212. u32 args[3];
  213. u32 opcode;
  214. u32 name_id;
  215. u8 charbuf[4];
  216. long long_tmp;
  217. u32 variable_id;
  218. u8 *charptr_tmp;
  219. u8 *charptr_tmp2;
  220. long *longptr_tmp;
  221. int version = 0;
  222. int delta = 0;
  223. int stack_ptr = 0;
  224. u32 arg_count;
  225. int done = 0;
  226. int bad_opcode = 0;
  227. u32 count;
  228. u32 index;
  229. u32 index2;
  230. s32 long_count;
  231. s32 long_idx;
  232. s32 long_idx2;
  233. u32 i;
  234. u32 j;
  235. u32 uncomp_size;
  236. u32 offset;
  237. u32 value;
  238. int current_proc = 0;
  239. int reverse;
  240. char *name;
  241. dprintk("%s\n", __func__);
  242. /* Read header information */
  243. if (program_size > 52L) {
  244. first_word = get_unaligned_be32(&p[0]);
  245. version = (first_word & 1L);
  246. *format_version = version + 1;
  247. delta = version * 8;
  248. action_table = get_unaligned_be32(&p[4]);
  249. proc_table = get_unaligned_be32(&p[8]);
  250. str_table = get_unaligned_be32(&p[4 + delta]);
  251. sym_table = get_unaligned_be32(&p[16 + delta]);
  252. data_sect = get_unaligned_be32(&p[20 + delta]);
  253. code_sect = get_unaligned_be32(&p[24 + delta]);
  254. debug_sect = get_unaligned_be32(&p[28 + delta]);
  255. action_count = get_unaligned_be32(&p[40 + delta]);
  256. proc_count = get_unaligned_be32(&p[44 + delta]);
  257. sym_count = get_unaligned_be32(&p[48 + (2 * delta)]);
  258. }
  259. if ((first_word != 0x4A414D00L) && (first_word != 0x4A414D01L)) {
  260. done = 1;
  261. status = -EIO;
  262. goto exit_done;
  263. }
  264. if (sym_count <= 0)
  265. goto exit_done;
  266. vars = kzalloc_objs(long, sym_count);
  267. if (vars == NULL)
  268. status = -ENOMEM;
  269. if (status == 0) {
  270. var_size = kzalloc_objs(s32, sym_count);
  271. if (var_size == NULL)
  272. status = -ENOMEM;
  273. }
  274. if (status == 0) {
  275. attrs = kzalloc(sym_count, GFP_KERNEL);
  276. if (attrs == NULL)
  277. status = -ENOMEM;
  278. }
  279. if ((status == 0) && (version > 0)) {
  280. proc_attributes = kzalloc(proc_count, GFP_KERNEL);
  281. if (proc_attributes == NULL)
  282. status = -ENOMEM;
  283. }
  284. if (status != 0)
  285. goto exit_done;
  286. delta = version * 2;
  287. for (i = 0; i < sym_count; ++i) {
  288. offset = (sym_table + ((11 + delta) * i));
  289. value = get_unaligned_be32(&p[offset + 3 + delta]);
  290. attrs[i] = p[offset];
  291. /*
  292. * use bit 7 of attribute byte to indicate that
  293. * this buffer was dynamically allocated
  294. * and should be freed later
  295. */
  296. attrs[i] &= 0x7f;
  297. var_size[i] = get_unaligned_be32(&p[offset + 7 + delta]);
  298. /*
  299. * Attribute bits:
  300. * bit 0: 0 = read-only, 1 = read-write
  301. * bit 1: 0 = not compressed, 1 = compressed
  302. * bit 2: 0 = not initialized, 1 = initialized
  303. * bit 3: 0 = scalar, 1 = array
  304. * bit 4: 0 = Boolean, 1 = integer
  305. * bit 5: 0 = declared variable,
  306. * 1 = compiler created temporary variable
  307. */
  308. if ((attrs[i] & 0x0c) == 0x04)
  309. /* initialized scalar variable */
  310. vars[i] = value;
  311. else if ((attrs[i] & 0x1e) == 0x0e) {
  312. /* initialized compressed Boolean array */
  313. uncomp_size = get_unaligned_le32(&p[data_sect + value]);
  314. /* allocate a buffer for the uncompressed data */
  315. vars[i] = (long)kzalloc(uncomp_size, GFP_KERNEL);
  316. if (vars[i] == 0L)
  317. status = -ENOMEM;
  318. else {
  319. /* set flag so buffer will be freed later */
  320. attrs[i] |= 0x80;
  321. /* uncompress the data */
  322. if (altera_shrink(&p[data_sect + value],
  323. var_size[i],
  324. (u8 *)vars[i],
  325. uncomp_size,
  326. version) != uncomp_size)
  327. /* decompression failed */
  328. status = -EIO;
  329. else
  330. var_size[i] = uncomp_size * 8L;
  331. }
  332. } else if ((attrs[i] & 0x1e) == 0x0c) {
  333. /* initialized Boolean array */
  334. vars[i] = value + data_sect + (long)p;
  335. } else if ((attrs[i] & 0x1c) == 0x1c) {
  336. /* initialized integer array */
  337. vars[i] = value + data_sect;
  338. } else if ((attrs[i] & 0x0c) == 0x08) {
  339. /* uninitialized array */
  340. /* flag attrs so that memory is freed */
  341. attrs[i] |= 0x80;
  342. if (var_size[i] > 0) {
  343. u32 size;
  344. if (attrs[i] & 0x10)
  345. /* integer array */
  346. size = (var_size[i] * sizeof(s32));
  347. else
  348. /* Boolean array */
  349. size = ((var_size[i] + 7L) / 8L);
  350. vars[i] = (long)kzalloc(size, GFP_KERNEL);
  351. if (vars[i] == 0) {
  352. status = -ENOMEM;
  353. } else {
  354. /* zero out memory */
  355. for (j = 0; j < size; ++j)
  356. ((u8 *)(vars[i]))[j] = 0;
  357. }
  358. } else
  359. vars[i] = 0;
  360. } else
  361. vars[i] = 0;
  362. }
  363. exit_done:
  364. if (status != 0)
  365. done = 1;
  366. altera_jinit(astate);
  367. pc = code_sect;
  368. msg_buff[0] = '\0';
  369. /*
  370. * For JBC version 2, we will execute the procedures corresponding to
  371. * the selected ACTION
  372. */
  373. if (version > 0) {
  374. if (aconf->action == NULL) {
  375. status = -EINVAL;
  376. done = 1;
  377. } else {
  378. int action_found = 0;
  379. for (i = 0; (i < action_count) && !action_found; ++i) {
  380. name_id = get_unaligned_be32(&p[action_table +
  381. (12 * i)]);
  382. name = &p[str_table + name_id];
  383. if (strncasecmp(aconf->action, name, strlen(name)) == 0) {
  384. action_found = 1;
  385. current_proc =
  386. get_unaligned_be32(&p[action_table +
  387. (12 * i) + 8]);
  388. }
  389. }
  390. if (!action_found) {
  391. status = -EINVAL;
  392. done = 1;
  393. }
  394. }
  395. if (status == 0) {
  396. int first_time = 1;
  397. i = current_proc;
  398. while ((i != 0) || first_time) {
  399. first_time = 0;
  400. /* check procedure attribute byte */
  401. proc_attributes[i] =
  402. (p[proc_table +
  403. (13 * i) + 8] &
  404. 0x03);
  405. /*
  406. * BIT0 - OPTIONAL
  407. * BIT1 - RECOMMENDED
  408. * BIT6 - FORCED OFF
  409. * BIT7 - FORCED ON
  410. */
  411. i = get_unaligned_be32(&p[proc_table +
  412. (13 * i) + 4]);
  413. }
  414. /*
  415. * Set current_proc to the first procedure
  416. * to be executed
  417. */
  418. i = current_proc;
  419. while ((i != 0) &&
  420. ((proc_attributes[i] == 1) ||
  421. ((proc_attributes[i] & 0xc0) == 0x40))) {
  422. i = get_unaligned_be32(&p[proc_table +
  423. (13 * i) + 4]);
  424. }
  425. if ((i != 0) || ((i == 0) && (current_proc == 0) &&
  426. ((proc_attributes[0] != 1) &&
  427. ((proc_attributes[0] & 0xc0) != 0x40)))) {
  428. current_proc = i;
  429. pc = code_sect +
  430. get_unaligned_be32(&p[proc_table +
  431. (13 * i) + 9]);
  432. if ((pc < code_sect) || (pc >= debug_sect))
  433. status = -ERANGE;
  434. } else
  435. /* there are no procedures to execute! */
  436. done = 1;
  437. }
  438. }
  439. msg_buff[0] = '\0';
  440. while (!done) {
  441. opcode = (p[pc] & 0xff);
  442. opcode_address = pc;
  443. ++pc;
  444. if (debug > 1)
  445. printk("opcode: %02x\n", opcode);
  446. arg_count = (opcode >> 6) & 3;
  447. for (i = 0; i < arg_count; ++i) {
  448. args[i] = get_unaligned_be32(&p[pc]);
  449. pc += 4;
  450. }
  451. switch (opcode) {
  452. case OP_NOP:
  453. break;
  454. case OP_DUP:
  455. if (altera_check_stack(stack_ptr, 1, &status)) {
  456. stack[stack_ptr] = stack[stack_ptr - 1];
  457. ++stack_ptr;
  458. }
  459. break;
  460. case OP_SWP:
  461. if (altera_check_stack(stack_ptr, 2, &status))
  462. swap(stack[stack_ptr - 2], stack[stack_ptr - 1]);
  463. break;
  464. case OP_ADD:
  465. if (altera_check_stack(stack_ptr, 2, &status)) {
  466. --stack_ptr;
  467. stack[stack_ptr - 1] += stack[stack_ptr];
  468. }
  469. break;
  470. case OP_SUB:
  471. if (altera_check_stack(stack_ptr, 2, &status)) {
  472. --stack_ptr;
  473. stack[stack_ptr - 1] -= stack[stack_ptr];
  474. }
  475. break;
  476. case OP_MULT:
  477. if (altera_check_stack(stack_ptr, 2, &status)) {
  478. --stack_ptr;
  479. stack[stack_ptr - 1] *= stack[stack_ptr];
  480. }
  481. break;
  482. case OP_DIV:
  483. if (altera_check_stack(stack_ptr, 2, &status)) {
  484. --stack_ptr;
  485. stack[stack_ptr - 1] /= stack[stack_ptr];
  486. }
  487. break;
  488. case OP_MOD:
  489. if (altera_check_stack(stack_ptr, 2, &status)) {
  490. --stack_ptr;
  491. stack[stack_ptr - 1] %= stack[stack_ptr];
  492. }
  493. break;
  494. case OP_SHL:
  495. if (altera_check_stack(stack_ptr, 2, &status)) {
  496. --stack_ptr;
  497. stack[stack_ptr - 1] <<= stack[stack_ptr];
  498. }
  499. break;
  500. case OP_SHR:
  501. if (altera_check_stack(stack_ptr, 2, &status)) {
  502. --stack_ptr;
  503. stack[stack_ptr - 1] >>= stack[stack_ptr];
  504. }
  505. break;
  506. case OP_NOT:
  507. if (altera_check_stack(stack_ptr, 1, &status))
  508. stack[stack_ptr - 1] ^= (-1L);
  509. break;
  510. case OP_AND:
  511. if (altera_check_stack(stack_ptr, 2, &status)) {
  512. --stack_ptr;
  513. stack[stack_ptr - 1] &= stack[stack_ptr];
  514. }
  515. break;
  516. case OP_OR:
  517. if (altera_check_stack(stack_ptr, 2, &status)) {
  518. --stack_ptr;
  519. stack[stack_ptr - 1] |= stack[stack_ptr];
  520. }
  521. break;
  522. case OP_XOR:
  523. if (altera_check_stack(stack_ptr, 2, &status)) {
  524. --stack_ptr;
  525. stack[stack_ptr - 1] ^= stack[stack_ptr];
  526. }
  527. break;
  528. case OP_INV:
  529. if (!altera_check_stack(stack_ptr, 1, &status))
  530. break;
  531. stack[stack_ptr - 1] = stack[stack_ptr - 1] ? 0L : 1L;
  532. break;
  533. case OP_GT:
  534. if (!altera_check_stack(stack_ptr, 2, &status))
  535. break;
  536. --stack_ptr;
  537. stack[stack_ptr - 1] =
  538. (stack[stack_ptr - 1] > stack[stack_ptr]) ?
  539. 1L : 0L;
  540. break;
  541. case OP_LT:
  542. if (!altera_check_stack(stack_ptr, 2, &status))
  543. break;
  544. --stack_ptr;
  545. stack[stack_ptr - 1] =
  546. (stack[stack_ptr - 1] < stack[stack_ptr]) ?
  547. 1L : 0L;
  548. break;
  549. case OP_RET:
  550. if ((version > 0) && (stack_ptr == 0)) {
  551. /*
  552. * We completed one of the main procedures
  553. * of an ACTION.
  554. * Find the next procedure
  555. * to be executed and jump to it.
  556. * If there are no more procedures, then EXIT.
  557. */
  558. i = get_unaligned_be32(&p[proc_table +
  559. (13 * current_proc) + 4]);
  560. while ((i != 0) &&
  561. ((proc_attributes[i] == 1) ||
  562. ((proc_attributes[i] & 0xc0) == 0x40)))
  563. i = get_unaligned_be32(&p[proc_table +
  564. (13 * i) + 4]);
  565. if (i == 0) {
  566. /* no procedures to execute! */
  567. done = 1;
  568. *exit_code = 0; /* success */
  569. } else {
  570. current_proc = i;
  571. pc = code_sect + get_unaligned_be32(
  572. &p[proc_table +
  573. (13 * i) + 9]);
  574. if ((pc < code_sect) ||
  575. (pc >= debug_sect))
  576. status = -ERANGE;
  577. }
  578. } else
  579. if (altera_check_stack(stack_ptr, 1, &status)) {
  580. pc = stack[--stack_ptr] + code_sect;
  581. if ((pc <= code_sect) ||
  582. (pc >= debug_sect))
  583. status = -ERANGE;
  584. }
  585. break;
  586. case OP_CMPS:
  587. /*
  588. * Array short compare
  589. * ...stack 0 is source 1 value
  590. * ...stack 1 is source 2 value
  591. * ...stack 2 is mask value
  592. * ...stack 3 is count
  593. */
  594. if (altera_check_stack(stack_ptr, 4, &status)) {
  595. s32 a = stack[--stack_ptr];
  596. s32 b = stack[--stack_ptr];
  597. long_tmp = stack[--stack_ptr];
  598. count = stack[stack_ptr - 1];
  599. if ((count < 1) || (count > 32))
  600. status = -ERANGE;
  601. else {
  602. long_tmp &= ((-1L) >> (32 - count));
  603. stack[stack_ptr - 1] =
  604. ((a & long_tmp) == (b & long_tmp))
  605. ? 1L : 0L;
  606. }
  607. }
  608. break;
  609. case OP_PINT:
  610. /*
  611. * PRINT add integer
  612. * ...stack 0 is integer value
  613. */
  614. if (!altera_check_stack(stack_ptr, 1, &status))
  615. break;
  616. sprintf(&msg_buff[strlen(msg_buff)],
  617. "%ld", stack[--stack_ptr]);
  618. break;
  619. case OP_PRNT:
  620. /* PRINT finish */
  621. if (debug)
  622. printk(msg_buff, "\n");
  623. msg_buff[0] = '\0';
  624. break;
  625. case OP_DSS:
  626. /*
  627. * DRSCAN short
  628. * ...stack 0 is scan data
  629. * ...stack 1 is count
  630. */
  631. if (!altera_check_stack(stack_ptr, 2, &status))
  632. break;
  633. long_tmp = stack[--stack_ptr];
  634. count = stack[--stack_ptr];
  635. put_unaligned_le32(long_tmp, &charbuf[0]);
  636. status = altera_drscan(astate, count, charbuf, 0);
  637. break;
  638. case OP_DSSC:
  639. /*
  640. * DRSCAN short with capture
  641. * ...stack 0 is scan data
  642. * ...stack 1 is count
  643. */
  644. if (!altera_check_stack(stack_ptr, 2, &status))
  645. break;
  646. long_tmp = stack[--stack_ptr];
  647. count = stack[stack_ptr - 1];
  648. put_unaligned_le32(long_tmp, &charbuf[0]);
  649. status = altera_swap_dr(astate, count, charbuf,
  650. 0, charbuf, 0);
  651. stack[stack_ptr - 1] = get_unaligned_le32(&charbuf[0]);
  652. break;
  653. case OP_ISS:
  654. /*
  655. * IRSCAN short
  656. * ...stack 0 is scan data
  657. * ...stack 1 is count
  658. */
  659. if (!altera_check_stack(stack_ptr, 2, &status))
  660. break;
  661. long_tmp = stack[--stack_ptr];
  662. count = stack[--stack_ptr];
  663. put_unaligned_le32(long_tmp, &charbuf[0]);
  664. status = altera_irscan(astate, count, charbuf, 0);
  665. break;
  666. case OP_ISSC:
  667. /*
  668. * IRSCAN short with capture
  669. * ...stack 0 is scan data
  670. * ...stack 1 is count
  671. */
  672. if (!altera_check_stack(stack_ptr, 2, &status))
  673. break;
  674. long_tmp = stack[--stack_ptr];
  675. count = stack[stack_ptr - 1];
  676. put_unaligned_le32(long_tmp, &charbuf[0]);
  677. status = altera_swap_ir(astate, count, charbuf,
  678. 0, charbuf, 0);
  679. stack[stack_ptr - 1] = get_unaligned_le32(&charbuf[0]);
  680. break;
  681. case OP_DPR:
  682. if (!altera_check_stack(stack_ptr, 1, &status))
  683. break;
  684. count = stack[--stack_ptr];
  685. status = altera_set_dr_pre(&astate->js, count, 0, NULL);
  686. break;
  687. case OP_DPRL:
  688. /*
  689. * DRPRE with literal data
  690. * ...stack 0 is count
  691. * ...stack 1 is literal data
  692. */
  693. if (!altera_check_stack(stack_ptr, 2, &status))
  694. break;
  695. count = stack[--stack_ptr];
  696. long_tmp = stack[--stack_ptr];
  697. put_unaligned_le32(long_tmp, &charbuf[0]);
  698. status = altera_set_dr_pre(&astate->js, count, 0,
  699. charbuf);
  700. break;
  701. case OP_DPO:
  702. /*
  703. * DRPOST
  704. * ...stack 0 is count
  705. */
  706. if (altera_check_stack(stack_ptr, 1, &status)) {
  707. count = stack[--stack_ptr];
  708. status = altera_set_dr_post(&astate->js, count,
  709. 0, NULL);
  710. }
  711. break;
  712. case OP_DPOL:
  713. /*
  714. * DRPOST with literal data
  715. * ...stack 0 is count
  716. * ...stack 1 is literal data
  717. */
  718. if (!altera_check_stack(stack_ptr, 2, &status))
  719. break;
  720. count = stack[--stack_ptr];
  721. long_tmp = stack[--stack_ptr];
  722. put_unaligned_le32(long_tmp, &charbuf[0]);
  723. status = altera_set_dr_post(&astate->js, count, 0,
  724. charbuf);
  725. break;
  726. case OP_IPR:
  727. if (altera_check_stack(stack_ptr, 1, &status)) {
  728. count = stack[--stack_ptr];
  729. status = altera_set_ir_pre(&astate->js, count,
  730. 0, NULL);
  731. }
  732. break;
  733. case OP_IPRL:
  734. /*
  735. * IRPRE with literal data
  736. * ...stack 0 is count
  737. * ...stack 1 is literal data
  738. */
  739. if (altera_check_stack(stack_ptr, 2, &status)) {
  740. count = stack[--stack_ptr];
  741. long_tmp = stack[--stack_ptr];
  742. put_unaligned_le32(long_tmp, &charbuf[0]);
  743. status = altera_set_ir_pre(&astate->js, count,
  744. 0, charbuf);
  745. }
  746. break;
  747. case OP_IPO:
  748. /*
  749. * IRPOST
  750. * ...stack 0 is count
  751. */
  752. if (altera_check_stack(stack_ptr, 1, &status)) {
  753. count = stack[--stack_ptr];
  754. status = altera_set_ir_post(&astate->js, count,
  755. 0, NULL);
  756. }
  757. break;
  758. case OP_IPOL:
  759. /*
  760. * IRPOST with literal data
  761. * ...stack 0 is count
  762. * ...stack 1 is literal data
  763. */
  764. if (!altera_check_stack(stack_ptr, 2, &status))
  765. break;
  766. count = stack[--stack_ptr];
  767. long_tmp = stack[--stack_ptr];
  768. put_unaligned_le32(long_tmp, &charbuf[0]);
  769. status = altera_set_ir_post(&astate->js, count, 0,
  770. charbuf);
  771. break;
  772. case OP_PCHR:
  773. if (altera_check_stack(stack_ptr, 1, &status)) {
  774. u8 ch;
  775. count = strlen(msg_buff);
  776. ch = (char) stack[--stack_ptr];
  777. if ((ch < 1) || (ch > 127)) {
  778. /*
  779. * character code out of range
  780. * instead of flagging an error,
  781. * force the value to 127
  782. */
  783. ch = 127;
  784. }
  785. msg_buff[count] = ch;
  786. msg_buff[count + 1] = '\0';
  787. }
  788. break;
  789. case OP_EXIT:
  790. if (altera_check_stack(stack_ptr, 1, &status))
  791. *exit_code = stack[--stack_ptr];
  792. done = 1;
  793. break;
  794. case OP_EQU:
  795. if (!altera_check_stack(stack_ptr, 2, &status))
  796. break;
  797. --stack_ptr;
  798. stack[stack_ptr - 1] =
  799. (stack[stack_ptr - 1] == stack[stack_ptr]) ?
  800. 1L : 0L;
  801. break;
  802. case OP_POPT:
  803. if (altera_check_stack(stack_ptr, 1, &status))
  804. --stack_ptr;
  805. break;
  806. case OP_ABS:
  807. if (!altera_check_stack(stack_ptr, 1, &status))
  808. break;
  809. if (stack[stack_ptr - 1] < 0)
  810. stack[stack_ptr - 1] = 0 - stack[stack_ptr - 1];
  811. break;
  812. case OP_BCH0:
  813. /*
  814. * Batch operation 0
  815. * SWP
  816. * SWPN 7
  817. * SWP
  818. * SWPN 6
  819. * DUPN 8
  820. * SWPN 2
  821. * SWP
  822. * DUPN 6
  823. * DUPN 6
  824. */
  825. /* SWP */
  826. if (altera_check_stack(stack_ptr, 2, &status))
  827. swap(stack[stack_ptr - 2], stack[stack_ptr - 1]);
  828. /* SWPN 7 */
  829. index = 7 + 1;
  830. if (altera_check_stack(stack_ptr, index, &status))
  831. swap(stack[stack_ptr - index], stack[stack_ptr - 1]);
  832. /* SWP */
  833. if (altera_check_stack(stack_ptr, 2, &status))
  834. swap(stack[stack_ptr - 2], stack[stack_ptr - 1]);
  835. /* SWPN 6 */
  836. index = 6 + 1;
  837. if (altera_check_stack(stack_ptr, index, &status))
  838. swap(stack[stack_ptr - index], stack[stack_ptr - 1]);
  839. /* DUPN 8 */
  840. index = 8 + 1;
  841. if (altera_check_stack(stack_ptr, index, &status)) {
  842. stack[stack_ptr] = stack[stack_ptr - index];
  843. ++stack_ptr;
  844. }
  845. /* SWPN 2 */
  846. index = 2 + 1;
  847. if (altera_check_stack(stack_ptr, index, &status))
  848. swap(stack[stack_ptr - index], stack[stack_ptr - 1]);
  849. /* SWP */
  850. if (altera_check_stack(stack_ptr, 2, &status))
  851. swap(stack[stack_ptr - 2], stack[stack_ptr - 1]);
  852. /* DUPN 6 */
  853. index = 6 + 1;
  854. if (altera_check_stack(stack_ptr, index, &status)) {
  855. stack[stack_ptr] = stack[stack_ptr - index];
  856. ++stack_ptr;
  857. }
  858. /* DUPN 6 */
  859. index = 6 + 1;
  860. if (altera_check_stack(stack_ptr, index, &status)) {
  861. stack[stack_ptr] = stack[stack_ptr - index];
  862. ++stack_ptr;
  863. }
  864. break;
  865. case OP_PSH0:
  866. stack[stack_ptr++] = 0;
  867. break;
  868. case OP_PSHL:
  869. stack[stack_ptr++] = (s32) args[0];
  870. break;
  871. case OP_PSHV:
  872. stack[stack_ptr++] = vars[args[0]];
  873. break;
  874. case OP_JMP:
  875. pc = args[0] + code_sect;
  876. if ((pc < code_sect) || (pc >= debug_sect))
  877. status = -ERANGE;
  878. break;
  879. case OP_CALL:
  880. stack[stack_ptr++] = pc;
  881. pc = args[0] + code_sect;
  882. if ((pc < code_sect) || (pc >= debug_sect))
  883. status = -ERANGE;
  884. break;
  885. case OP_NEXT:
  886. /*
  887. * Process FOR / NEXT loop
  888. * ...argument 0 is variable ID
  889. * ...stack 0 is step value
  890. * ...stack 1 is end value
  891. * ...stack 2 is top address
  892. */
  893. if (altera_check_stack(stack_ptr, 3, &status)) {
  894. s32 step = stack[stack_ptr - 1];
  895. s32 end = stack[stack_ptr - 2];
  896. s32 top = stack[stack_ptr - 3];
  897. s32 iterator = vars[args[0]];
  898. int break_out = 0;
  899. if (step < 0) {
  900. if (iterator <= end)
  901. break_out = 1;
  902. } else if (iterator >= end)
  903. break_out = 1;
  904. if (break_out) {
  905. stack_ptr -= 3;
  906. } else {
  907. vars[args[0]] = iterator + step;
  908. pc = top + code_sect;
  909. if ((pc < code_sect) ||
  910. (pc >= debug_sect))
  911. status = -ERANGE;
  912. }
  913. }
  914. break;
  915. case OP_PSTR:
  916. /*
  917. * PRINT add string
  918. * ...argument 0 is string ID
  919. */
  920. count = strlen(msg_buff);
  921. strscpy(&msg_buff[count],
  922. &p[str_table + args[0]],
  923. ALTERA_MESSAGE_LENGTH - count);
  924. break;
  925. case OP_SINT:
  926. /*
  927. * STATE intermediate state
  928. * ...argument 0 is state code
  929. */
  930. status = altera_goto_jstate(astate, args[0]);
  931. break;
  932. case OP_ST:
  933. /*
  934. * STATE final state
  935. * ...argument 0 is state code
  936. */
  937. status = altera_goto_jstate(astate, args[0]);
  938. break;
  939. case OP_ISTP:
  940. /*
  941. * IRSTOP state
  942. * ...argument 0 is state code
  943. */
  944. status = altera_set_irstop(&astate->js, args[0]);
  945. break;
  946. case OP_DSTP:
  947. /*
  948. * DRSTOP state
  949. * ...argument 0 is state code
  950. */
  951. status = altera_set_drstop(&astate->js, args[0]);
  952. break;
  953. case OP_SWPN:
  954. /*
  955. * Exchange top with Nth stack value
  956. * ...argument 0 is 0-based stack entry
  957. * to swap with top element
  958. */
  959. index = (args[0]) + 1;
  960. if (altera_check_stack(stack_ptr, index, &status))
  961. swap(stack[stack_ptr - index], stack[stack_ptr - 1]);
  962. break;
  963. case OP_DUPN:
  964. /*
  965. * Duplicate Nth stack value
  966. * ...argument 0 is 0-based stack entry to duplicate
  967. */
  968. index = (args[0]) + 1;
  969. if (altera_check_stack(stack_ptr, index, &status)) {
  970. stack[stack_ptr] = stack[stack_ptr - index];
  971. ++stack_ptr;
  972. }
  973. break;
  974. case OP_POPV:
  975. /*
  976. * Pop stack into scalar variable
  977. * ...argument 0 is variable ID
  978. * ...stack 0 is value
  979. */
  980. if (altera_check_stack(stack_ptr, 1, &status))
  981. vars[args[0]] = stack[--stack_ptr];
  982. break;
  983. case OP_POPE:
  984. /*
  985. * Pop stack into integer array element
  986. * ...argument 0 is variable ID
  987. * ...stack 0 is array index
  988. * ...stack 1 is value
  989. */
  990. if (!altera_check_stack(stack_ptr, 2, &status))
  991. break;
  992. variable_id = args[0];
  993. /*
  994. * If variable is read-only,
  995. * convert to writable array
  996. */
  997. if ((version > 0) &&
  998. ((attrs[variable_id] & 0x9c) == 0x1c)) {
  999. /* Allocate a writable buffer for this array */
  1000. count = var_size[variable_id];
  1001. long_tmp = vars[variable_id];
  1002. longptr_tmp = kzalloc_objs(long, count);
  1003. vars[variable_id] = (long)longptr_tmp;
  1004. if (vars[variable_id] == 0) {
  1005. status = -ENOMEM;
  1006. break;
  1007. }
  1008. /* copy previous contents into buffer */
  1009. for (i = 0; i < count; ++i) {
  1010. longptr_tmp[i] =
  1011. get_unaligned_be32(&p[long_tmp]);
  1012. long_tmp += sizeof(long);
  1013. }
  1014. /*
  1015. * set bit 7 - buffer was
  1016. * dynamically allocated
  1017. */
  1018. attrs[variable_id] |= 0x80;
  1019. /* clear bit 2 - variable is writable */
  1020. attrs[variable_id] &= ~0x04;
  1021. attrs[variable_id] |= 0x01;
  1022. }
  1023. /* check that variable is a writable integer array */
  1024. if ((attrs[variable_id] & 0x1c) != 0x18)
  1025. status = -ERANGE;
  1026. else {
  1027. longptr_tmp = (long *)vars[variable_id];
  1028. /* pop the array index */
  1029. index = stack[--stack_ptr];
  1030. /* pop the value and store it into the array */
  1031. longptr_tmp[index] = stack[--stack_ptr];
  1032. }
  1033. break;
  1034. case OP_POPA:
  1035. /*
  1036. * Pop stack into Boolean array
  1037. * ...argument 0 is variable ID
  1038. * ...stack 0 is count
  1039. * ...stack 1 is array index
  1040. * ...stack 2 is value
  1041. */
  1042. if (!altera_check_stack(stack_ptr, 3, &status))
  1043. break;
  1044. variable_id = args[0];
  1045. /*
  1046. * If variable is read-only,
  1047. * convert to writable array
  1048. */
  1049. if ((version > 0) &&
  1050. ((attrs[variable_id] & 0x9c) == 0x0c)) {
  1051. /* Allocate a writable buffer for this array */
  1052. long_tmp =
  1053. (var_size[variable_id] + 7L) >> 3L;
  1054. charptr_tmp2 = (u8 *)vars[variable_id];
  1055. charptr_tmp =
  1056. kzalloc(long_tmp, GFP_KERNEL);
  1057. vars[variable_id] = (long)charptr_tmp;
  1058. if (vars[variable_id] == 0) {
  1059. status = -ENOMEM;
  1060. break;
  1061. }
  1062. /* zero the buffer */
  1063. for (long_idx = 0L;
  1064. long_idx < long_tmp;
  1065. ++long_idx) {
  1066. charptr_tmp[long_idx] = 0;
  1067. }
  1068. /* copy previous contents into buffer */
  1069. for (long_idx = 0L;
  1070. long_idx < var_size[variable_id];
  1071. ++long_idx) {
  1072. long_idx2 = long_idx;
  1073. if (charptr_tmp2[long_idx2 >> 3] &
  1074. (1 << (long_idx2 & 7))) {
  1075. charptr_tmp[long_idx >> 3] |=
  1076. (1 << (long_idx & 7));
  1077. }
  1078. }
  1079. /*
  1080. * set bit 7 - buffer was
  1081. * dynamically allocated
  1082. */
  1083. attrs[variable_id] |= 0x80;
  1084. /* clear bit 2 - variable is writable */
  1085. attrs[variable_id] &= ~0x04;
  1086. attrs[variable_id] |= 0x01;
  1087. }
  1088. /*
  1089. * check that variable is
  1090. * a writable Boolean array
  1091. */
  1092. if ((attrs[variable_id] & 0x1c) != 0x08) {
  1093. status = -ERANGE;
  1094. break;
  1095. }
  1096. charptr_tmp = (u8 *)vars[variable_id];
  1097. /* pop the count (number of bits to copy) */
  1098. long_count = stack[--stack_ptr];
  1099. /* pop the array index */
  1100. long_idx = stack[--stack_ptr];
  1101. reverse = 0;
  1102. if (version > 0) {
  1103. /*
  1104. * stack 0 = array right index
  1105. * stack 1 = array left index
  1106. */
  1107. if (long_idx > long_count) {
  1108. reverse = 1;
  1109. long_tmp = long_count;
  1110. long_count = 1 + long_idx -
  1111. long_count;
  1112. long_idx = long_tmp;
  1113. /* reverse POPA is not supported */
  1114. status = -ERANGE;
  1115. break;
  1116. } else
  1117. long_count = 1 + long_count -
  1118. long_idx;
  1119. }
  1120. /* pop the data */
  1121. long_tmp = stack[--stack_ptr];
  1122. if (long_count < 1) {
  1123. status = -ERANGE;
  1124. break;
  1125. }
  1126. for (i = 0; i < long_count; ++i) {
  1127. if (long_tmp & (1L << (s32) i))
  1128. charptr_tmp[long_idx >> 3L] |=
  1129. (1L << (long_idx & 7L));
  1130. else
  1131. charptr_tmp[long_idx >> 3L] &=
  1132. ~(1L << (long_idx & 7L));
  1133. ++long_idx;
  1134. }
  1135. break;
  1136. case OP_JMPZ:
  1137. /*
  1138. * Pop stack and branch if zero
  1139. * ...argument 0 is address
  1140. * ...stack 0 is condition value
  1141. */
  1142. if (altera_check_stack(stack_ptr, 1, &status)) {
  1143. if (stack[--stack_ptr] == 0) {
  1144. pc = args[0] + code_sect;
  1145. if ((pc < code_sect) ||
  1146. (pc >= debug_sect))
  1147. status = -ERANGE;
  1148. }
  1149. }
  1150. break;
  1151. case OP_DS:
  1152. case OP_IS:
  1153. /*
  1154. * DRSCAN
  1155. * IRSCAN
  1156. * ...argument 0 is scan data variable ID
  1157. * ...stack 0 is array index
  1158. * ...stack 1 is count
  1159. */
  1160. if (!altera_check_stack(stack_ptr, 2, &status))
  1161. break;
  1162. long_idx = stack[--stack_ptr];
  1163. long_count = stack[--stack_ptr];
  1164. reverse = 0;
  1165. if (version > 0) {
  1166. /*
  1167. * stack 0 = array right index
  1168. * stack 1 = array left index
  1169. * stack 2 = count
  1170. */
  1171. long_tmp = long_count;
  1172. long_count = stack[--stack_ptr];
  1173. if (long_idx > long_tmp) {
  1174. reverse = 1;
  1175. long_idx = long_tmp;
  1176. }
  1177. }
  1178. charptr_tmp = (u8 *)vars[args[0]];
  1179. if (reverse) {
  1180. /*
  1181. * allocate a buffer
  1182. * and reverse the data order
  1183. */
  1184. charptr_tmp2 = charptr_tmp;
  1185. charptr_tmp = kzalloc((long_count >> 3) + 1,
  1186. GFP_KERNEL);
  1187. if (charptr_tmp == NULL) {
  1188. status = -ENOMEM;
  1189. break;
  1190. }
  1191. long_tmp = long_idx + long_count - 1;
  1192. long_idx2 = 0;
  1193. while (long_idx2 < long_count) {
  1194. if (charptr_tmp2[long_tmp >> 3] &
  1195. (1 << (long_tmp & 7)))
  1196. charptr_tmp[long_idx2 >> 3] |=
  1197. (1 << (long_idx2 & 7));
  1198. else
  1199. charptr_tmp[long_idx2 >> 3] &=
  1200. ~(1 << (long_idx2 & 7));
  1201. --long_tmp;
  1202. ++long_idx2;
  1203. }
  1204. }
  1205. if (opcode == 0x51) /* DS */
  1206. status = altera_drscan(astate, long_count,
  1207. charptr_tmp, long_idx);
  1208. else /* IS */
  1209. status = altera_irscan(astate, long_count,
  1210. charptr_tmp, long_idx);
  1211. if (reverse)
  1212. kfree(charptr_tmp);
  1213. break;
  1214. case OP_DPRA:
  1215. /*
  1216. * DRPRE with array data
  1217. * ...argument 0 is variable ID
  1218. * ...stack 0 is array index
  1219. * ...stack 1 is count
  1220. */
  1221. if (!altera_check_stack(stack_ptr, 2, &status))
  1222. break;
  1223. index = stack[--stack_ptr];
  1224. count = stack[--stack_ptr];
  1225. if (version > 0)
  1226. /*
  1227. * stack 0 = array right index
  1228. * stack 1 = array left index
  1229. */
  1230. count = 1 + count - index;
  1231. charptr_tmp = (u8 *)vars[args[0]];
  1232. status = altera_set_dr_pre(&astate->js, count, index,
  1233. charptr_tmp);
  1234. break;
  1235. case OP_DPOA:
  1236. /*
  1237. * DRPOST with array data
  1238. * ...argument 0 is variable ID
  1239. * ...stack 0 is array index
  1240. * ...stack 1 is count
  1241. */
  1242. if (!altera_check_stack(stack_ptr, 2, &status))
  1243. break;
  1244. index = stack[--stack_ptr];
  1245. count = stack[--stack_ptr];
  1246. if (version > 0)
  1247. /*
  1248. * stack 0 = array right index
  1249. * stack 1 = array left index
  1250. */
  1251. count = 1 + count - index;
  1252. charptr_tmp = (u8 *)vars[args[0]];
  1253. status = altera_set_dr_post(&astate->js, count, index,
  1254. charptr_tmp);
  1255. break;
  1256. case OP_IPRA:
  1257. /*
  1258. * IRPRE with array data
  1259. * ...argument 0 is variable ID
  1260. * ...stack 0 is array index
  1261. * ...stack 1 is count
  1262. */
  1263. if (!altera_check_stack(stack_ptr, 2, &status))
  1264. break;
  1265. index = stack[--stack_ptr];
  1266. count = stack[--stack_ptr];
  1267. if (version > 0)
  1268. /*
  1269. * stack 0 = array right index
  1270. * stack 1 = array left index
  1271. */
  1272. count = 1 + count - index;
  1273. charptr_tmp = (u8 *)vars[args[0]];
  1274. status = altera_set_ir_pre(&astate->js, count, index,
  1275. charptr_tmp);
  1276. break;
  1277. case OP_IPOA:
  1278. /*
  1279. * IRPOST with array data
  1280. * ...argument 0 is variable ID
  1281. * ...stack 0 is array index
  1282. * ...stack 1 is count
  1283. */
  1284. if (!altera_check_stack(stack_ptr, 2, &status))
  1285. break;
  1286. index = stack[--stack_ptr];
  1287. count = stack[--stack_ptr];
  1288. if (version > 0)
  1289. /*
  1290. * stack 0 = array right index
  1291. * stack 1 = array left index
  1292. */
  1293. count = 1 + count - index;
  1294. charptr_tmp = (u8 *)vars[args[0]];
  1295. status = altera_set_ir_post(&astate->js, count, index,
  1296. charptr_tmp);
  1297. break;
  1298. case OP_EXPT:
  1299. /*
  1300. * EXPORT
  1301. * ...argument 0 is string ID
  1302. * ...stack 0 is integer expression
  1303. */
  1304. if (altera_check_stack(stack_ptr, 1, &status)) {
  1305. name = &p[str_table + args[0]];
  1306. long_tmp = stack[--stack_ptr];
  1307. altera_export_int(name, long_tmp);
  1308. }
  1309. break;
  1310. case OP_PSHE:
  1311. /*
  1312. * Push integer array element
  1313. * ...argument 0 is variable ID
  1314. * ...stack 0 is array index
  1315. */
  1316. if (!altera_check_stack(stack_ptr, 1, &status))
  1317. break;
  1318. variable_id = args[0];
  1319. index = stack[stack_ptr - 1];
  1320. /* check variable type */
  1321. if ((attrs[variable_id] & 0x1f) == 0x19) {
  1322. /* writable integer array */
  1323. longptr_tmp = (long *)vars[variable_id];
  1324. stack[stack_ptr - 1] = longptr_tmp[index];
  1325. } else if ((attrs[variable_id] & 0x1f) == 0x1c) {
  1326. /* read-only integer array */
  1327. long_tmp = vars[variable_id] +
  1328. (index * sizeof(long));
  1329. stack[stack_ptr - 1] =
  1330. get_unaligned_be32(&p[long_tmp]);
  1331. } else
  1332. status = -ERANGE;
  1333. break;
  1334. case OP_PSHA:
  1335. /*
  1336. * Push Boolean array
  1337. * ...argument 0 is variable ID
  1338. * ...stack 0 is count
  1339. * ...stack 1 is array index
  1340. */
  1341. if (!altera_check_stack(stack_ptr, 2, &status))
  1342. break;
  1343. variable_id = args[0];
  1344. /* check that variable is a Boolean array */
  1345. if ((attrs[variable_id] & 0x18) != 0x08) {
  1346. status = -ERANGE;
  1347. break;
  1348. }
  1349. charptr_tmp = (u8 *)vars[variable_id];
  1350. /* pop the count (number of bits to copy) */
  1351. count = stack[--stack_ptr];
  1352. /* pop the array index */
  1353. index = stack[stack_ptr - 1];
  1354. if (version > 0)
  1355. /*
  1356. * stack 0 = array right index
  1357. * stack 1 = array left index
  1358. */
  1359. count = 1 + count - index;
  1360. if ((count < 1) || (count > 32)) {
  1361. status = -ERANGE;
  1362. break;
  1363. }
  1364. long_tmp = 0L;
  1365. for (i = 0; i < count; ++i)
  1366. if (charptr_tmp[(i + index) >> 3] &
  1367. (1 << ((i + index) & 7)))
  1368. long_tmp |= (1L << i);
  1369. stack[stack_ptr - 1] = long_tmp;
  1370. break;
  1371. case OP_DYNA:
  1372. /*
  1373. * Dynamically change size of array
  1374. * ...argument 0 is variable ID
  1375. * ...stack 0 is new size
  1376. */
  1377. if (!altera_check_stack(stack_ptr, 1, &status))
  1378. break;
  1379. variable_id = args[0];
  1380. long_tmp = stack[--stack_ptr];
  1381. if (long_tmp > var_size[variable_id]) {
  1382. var_size[variable_id] = long_tmp;
  1383. if (attrs[variable_id] & 0x10)
  1384. /* allocate integer array */
  1385. long_tmp *= sizeof(long);
  1386. else
  1387. /* allocate Boolean array */
  1388. long_tmp = (long_tmp + 7) >> 3;
  1389. /*
  1390. * If the buffer was previously allocated,
  1391. * free it
  1392. */
  1393. if (attrs[variable_id] & 0x80) {
  1394. kfree((void *)vars[variable_id]);
  1395. vars[variable_id] = 0;
  1396. }
  1397. /*
  1398. * Allocate a new buffer
  1399. * of the requested size
  1400. */
  1401. vars[variable_id] = (long)
  1402. kzalloc(long_tmp, GFP_KERNEL);
  1403. if (vars[variable_id] == 0) {
  1404. status = -ENOMEM;
  1405. break;
  1406. }
  1407. /*
  1408. * Set the attribute bit to indicate that
  1409. * this buffer was dynamically allocated and
  1410. * should be freed later
  1411. */
  1412. attrs[variable_id] |= 0x80;
  1413. /* zero out memory */
  1414. count = ((var_size[variable_id] + 7L) /
  1415. 8L);
  1416. charptr_tmp = (u8 *)(vars[variable_id]);
  1417. for (index = 0; index < count; ++index)
  1418. charptr_tmp[index] = 0;
  1419. }
  1420. break;
  1421. case OP_EXPV:
  1422. /*
  1423. * Export Boolean array
  1424. * ...argument 0 is string ID
  1425. * ...stack 0 is variable ID
  1426. * ...stack 1 is array right index
  1427. * ...stack 2 is array left index
  1428. */
  1429. if (!altera_check_stack(stack_ptr, 3, &status))
  1430. break;
  1431. if (version == 0) {
  1432. /* EXPV is not supported in JBC 1.0 */
  1433. bad_opcode = 1;
  1434. break;
  1435. }
  1436. name = &p[str_table + args[0]];
  1437. variable_id = stack[--stack_ptr];
  1438. long_idx = stack[--stack_ptr];/* right indx */
  1439. long_idx2 = stack[--stack_ptr];/* left indx */
  1440. if (long_idx > long_idx2) {
  1441. /* reverse indices not supported */
  1442. status = -ERANGE;
  1443. break;
  1444. }
  1445. long_count = 1 + long_idx2 - long_idx;
  1446. charptr_tmp = (u8 *)vars[variable_id];
  1447. charptr_tmp2 = NULL;
  1448. if ((long_idx & 7L) != 0) {
  1449. s32 k = long_idx;
  1450. charptr_tmp2 =
  1451. kzalloc(((long_count + 7L) / 8L),
  1452. GFP_KERNEL);
  1453. if (charptr_tmp2 == NULL) {
  1454. status = -ENOMEM;
  1455. break;
  1456. }
  1457. for (i = 0; i < long_count; ++i) {
  1458. if (charptr_tmp[k >> 3] &
  1459. (1 << (k & 7)))
  1460. charptr_tmp2[i >> 3] |=
  1461. (1 << (i & 7));
  1462. else
  1463. charptr_tmp2[i >> 3] &=
  1464. ~(1 << (i & 7));
  1465. ++k;
  1466. }
  1467. charptr_tmp = charptr_tmp2;
  1468. } else if (long_idx != 0)
  1469. charptr_tmp = &charptr_tmp[long_idx >> 3];
  1470. altera_export_bool_array(name, charptr_tmp,
  1471. long_count);
  1472. /* free allocated buffer */
  1473. if ((long_idx & 7L) != 0)
  1474. kfree(charptr_tmp2);
  1475. break;
  1476. case OP_COPY: {
  1477. /*
  1478. * Array copy
  1479. * ...argument 0 is dest ID
  1480. * ...argument 1 is source ID
  1481. * ...stack 0 is count
  1482. * ...stack 1 is dest index
  1483. * ...stack 2 is source index
  1484. */
  1485. s32 copy_count;
  1486. s32 copy_index;
  1487. s32 copy_index2;
  1488. s32 destleft;
  1489. s32 src_count;
  1490. s32 dest_count;
  1491. int src_reverse = 0;
  1492. int dest_reverse = 0;
  1493. if (!altera_check_stack(stack_ptr, 3, &status))
  1494. break;
  1495. copy_count = stack[--stack_ptr];
  1496. copy_index = stack[--stack_ptr];
  1497. copy_index2 = stack[--stack_ptr];
  1498. reverse = 0;
  1499. if (version > 0) {
  1500. /*
  1501. * stack 0 = source right index
  1502. * stack 1 = source left index
  1503. * stack 2 = destination right index
  1504. * stack 3 = destination left index
  1505. */
  1506. destleft = stack[--stack_ptr];
  1507. if (copy_count > copy_index) {
  1508. src_reverse = 1;
  1509. reverse = 1;
  1510. src_count = 1 + copy_count - copy_index;
  1511. /* copy_index = source start index */
  1512. } else {
  1513. src_count = 1 + copy_index - copy_count;
  1514. /* source start index */
  1515. copy_index = copy_count;
  1516. }
  1517. if (copy_index2 > destleft) {
  1518. dest_reverse = 1;
  1519. reverse = !reverse;
  1520. dest_count = 1 + copy_index2 - destleft;
  1521. /* destination start index */
  1522. copy_index2 = destleft;
  1523. } else
  1524. dest_count = 1 + destleft - copy_index2;
  1525. copy_count = (src_count < dest_count) ?
  1526. src_count : dest_count;
  1527. if ((src_reverse || dest_reverse) &&
  1528. (src_count != dest_count))
  1529. /*
  1530. * If either the source or destination
  1531. * is reversed, we can't tolerate
  1532. * a length mismatch, because we
  1533. * "left justify" arrays when copying.
  1534. * This won't work correctly
  1535. * with reversed arrays.
  1536. */
  1537. status = -ERANGE;
  1538. }
  1539. count = copy_count;
  1540. index = copy_index;
  1541. index2 = copy_index2;
  1542. /*
  1543. * If destination is a read-only array,
  1544. * allocate a buffer and convert it to a writable array
  1545. */
  1546. variable_id = args[1];
  1547. if ((version > 0) &&
  1548. ((attrs[variable_id] & 0x9c) == 0x0c)) {
  1549. /* Allocate a writable buffer for this array */
  1550. long_tmp =
  1551. (var_size[variable_id] + 7L) >> 3L;
  1552. charptr_tmp2 = (u8 *)vars[variable_id];
  1553. charptr_tmp =
  1554. kzalloc(long_tmp, GFP_KERNEL);
  1555. vars[variable_id] = (long)charptr_tmp;
  1556. if (vars[variable_id] == 0) {
  1557. status = -ENOMEM;
  1558. break;
  1559. }
  1560. /* zero the buffer */
  1561. for (long_idx = 0L; long_idx < long_tmp;
  1562. ++long_idx)
  1563. charptr_tmp[long_idx] = 0;
  1564. /* copy previous contents into buffer */
  1565. for (long_idx = 0L;
  1566. long_idx < var_size[variable_id];
  1567. ++long_idx) {
  1568. long_idx2 = long_idx;
  1569. if (charptr_tmp2[long_idx2 >> 3] &
  1570. (1 << (long_idx2 & 7)))
  1571. charptr_tmp[long_idx >> 3] |=
  1572. (1 << (long_idx & 7));
  1573. }
  1574. /*
  1575. set bit 7 - buffer was dynamically allocated */
  1576. attrs[variable_id] |= 0x80;
  1577. /* clear bit 2 - variable is writable */
  1578. attrs[variable_id] &= ~0x04;
  1579. attrs[variable_id] |= 0x01;
  1580. }
  1581. charptr_tmp = (u8 *)vars[args[1]];
  1582. charptr_tmp2 = (u8 *)vars[args[0]];
  1583. /* check if destination is a writable Boolean array */
  1584. if ((attrs[args[1]] & 0x1c) != 0x08) {
  1585. status = -ERANGE;
  1586. break;
  1587. }
  1588. if (count < 1) {
  1589. status = -ERANGE;
  1590. break;
  1591. }
  1592. if (reverse)
  1593. index2 += (count - 1);
  1594. for (i = 0; i < count; ++i) {
  1595. if (charptr_tmp2[index >> 3] &
  1596. (1 << (index & 7)))
  1597. charptr_tmp[index2 >> 3] |=
  1598. (1 << (index2 & 7));
  1599. else
  1600. charptr_tmp[index2 >> 3] &=
  1601. ~(1 << (index2 & 7));
  1602. ++index;
  1603. if (reverse)
  1604. --index2;
  1605. else
  1606. ++index2;
  1607. }
  1608. break;
  1609. }
  1610. case OP_DSC:
  1611. case OP_ISC: {
  1612. /*
  1613. * DRSCAN with capture
  1614. * IRSCAN with capture
  1615. * ...argument 0 is scan data variable ID
  1616. * ...argument 1 is capture variable ID
  1617. * ...stack 0 is capture index
  1618. * ...stack 1 is scan data index
  1619. * ...stack 2 is count
  1620. */
  1621. s32 scan_right, scan_left;
  1622. s32 capture_count = 0;
  1623. s32 scan_count = 0;
  1624. s32 capture_index;
  1625. s32 scan_index;
  1626. if (!altera_check_stack(stack_ptr, 3, &status))
  1627. break;
  1628. capture_index = stack[--stack_ptr];
  1629. scan_index = stack[--stack_ptr];
  1630. if (version > 0) {
  1631. /*
  1632. * stack 0 = capture right index
  1633. * stack 1 = capture left index
  1634. * stack 2 = scan right index
  1635. * stack 3 = scan left index
  1636. * stack 4 = count
  1637. */
  1638. scan_right = stack[--stack_ptr];
  1639. scan_left = stack[--stack_ptr];
  1640. capture_count = 1 + scan_index - capture_index;
  1641. scan_count = 1 + scan_left - scan_right;
  1642. scan_index = scan_right;
  1643. }
  1644. long_count = stack[--stack_ptr];
  1645. /*
  1646. * If capture array is read-only, allocate a buffer
  1647. * and convert it to a writable array
  1648. */
  1649. variable_id = args[1];
  1650. if ((version > 0) &&
  1651. ((attrs[variable_id] & 0x9c) == 0x0c)) {
  1652. /* Allocate a writable buffer for this array */
  1653. long_tmp =
  1654. (var_size[variable_id] + 7L) >> 3L;
  1655. charptr_tmp2 = (u8 *)vars[variable_id];
  1656. charptr_tmp =
  1657. kzalloc(long_tmp, GFP_KERNEL);
  1658. vars[variable_id] = (long)charptr_tmp;
  1659. if (vars[variable_id] == 0) {
  1660. status = -ENOMEM;
  1661. break;
  1662. }
  1663. /* zero the buffer */
  1664. for (long_idx = 0L; long_idx < long_tmp;
  1665. ++long_idx)
  1666. charptr_tmp[long_idx] = 0;
  1667. /* copy previous contents into buffer */
  1668. for (long_idx = 0L;
  1669. long_idx < var_size[variable_id];
  1670. ++long_idx) {
  1671. long_idx2 = long_idx;
  1672. if (charptr_tmp2[long_idx2 >> 3] &
  1673. (1 << (long_idx2 & 7)))
  1674. charptr_tmp[long_idx >> 3] |=
  1675. (1 << (long_idx & 7));
  1676. }
  1677. /*
  1678. * set bit 7 - buffer was
  1679. * dynamically allocated
  1680. */
  1681. attrs[variable_id] |= 0x80;
  1682. /* clear bit 2 - variable is writable */
  1683. attrs[variable_id] &= ~0x04;
  1684. attrs[variable_id] |= 0x01;
  1685. }
  1686. charptr_tmp = (u8 *)vars[args[0]];
  1687. charptr_tmp2 = (u8 *)vars[args[1]];
  1688. if ((version > 0) &&
  1689. ((long_count > capture_count) ||
  1690. (long_count > scan_count))) {
  1691. status = -ERANGE;
  1692. break;
  1693. }
  1694. /*
  1695. * check that capture array
  1696. * is a writable Boolean array
  1697. */
  1698. if ((attrs[args[1]] & 0x1c) != 0x08) {
  1699. status = -ERANGE;
  1700. break;
  1701. }
  1702. if (status == 0) {
  1703. if (opcode == 0x82) /* DSC */
  1704. status = altera_swap_dr(astate,
  1705. long_count,
  1706. charptr_tmp,
  1707. scan_index,
  1708. charptr_tmp2,
  1709. capture_index);
  1710. else /* ISC */
  1711. status = altera_swap_ir(astate,
  1712. long_count,
  1713. charptr_tmp,
  1714. scan_index,
  1715. charptr_tmp2,
  1716. capture_index);
  1717. }
  1718. break;
  1719. }
  1720. case OP_WAIT:
  1721. /*
  1722. * WAIT
  1723. * ...argument 0 is wait state
  1724. * ...argument 1 is end state
  1725. * ...stack 0 is cycles
  1726. * ...stack 1 is microseconds
  1727. */
  1728. if (!altera_check_stack(stack_ptr, 2, &status))
  1729. break;
  1730. long_tmp = stack[--stack_ptr];
  1731. if (long_tmp != 0L)
  1732. status = altera_wait_cycles(astate, long_tmp,
  1733. args[0]);
  1734. long_tmp = stack[--stack_ptr];
  1735. if ((status == 0) && (long_tmp != 0L))
  1736. status = altera_wait_msecs(astate,
  1737. long_tmp,
  1738. args[0]);
  1739. if ((status == 0) && (args[1] != args[0]))
  1740. status = altera_goto_jstate(astate,
  1741. args[1]);
  1742. if (version > 0) {
  1743. --stack_ptr; /* throw away MAX cycles */
  1744. --stack_ptr; /* throw away MAX microseconds */
  1745. }
  1746. break;
  1747. case OP_CMPA: {
  1748. /*
  1749. * Array compare
  1750. * ...argument 0 is source 1 ID
  1751. * ...argument 1 is source 2 ID
  1752. * ...argument 2 is mask ID
  1753. * ...stack 0 is source 1 index
  1754. * ...stack 1 is source 2 index
  1755. * ...stack 2 is mask index
  1756. * ...stack 3 is count
  1757. */
  1758. s32 a, b;
  1759. u8 *source1 = (u8 *)vars[args[0]];
  1760. u8 *source2 = (u8 *)vars[args[1]];
  1761. u8 *mask = (u8 *)vars[args[2]];
  1762. u32 index1;
  1763. u32 index2;
  1764. u32 mask_index;
  1765. if (!altera_check_stack(stack_ptr, 4, &status))
  1766. break;
  1767. index1 = stack[--stack_ptr];
  1768. index2 = stack[--stack_ptr];
  1769. mask_index = stack[--stack_ptr];
  1770. long_count = stack[--stack_ptr];
  1771. if (version > 0) {
  1772. /*
  1773. * stack 0 = source 1 right index
  1774. * stack 1 = source 1 left index
  1775. * stack 2 = source 2 right index
  1776. * stack 3 = source 2 left index
  1777. * stack 4 = mask right index
  1778. * stack 5 = mask left index
  1779. */
  1780. s32 mask_right = stack[--stack_ptr];
  1781. s32 mask_left = stack[--stack_ptr];
  1782. /* source 1 count */
  1783. a = 1 + index2 - index1;
  1784. /* source 2 count */
  1785. b = 1 + long_count - mask_index;
  1786. a = (a < b) ? a : b;
  1787. /* mask count */
  1788. b = 1 + mask_left - mask_right;
  1789. a = (a < b) ? a : b;
  1790. /* source 2 start index */
  1791. index2 = mask_index;
  1792. /* mask start index */
  1793. mask_index = mask_right;
  1794. long_count = a;
  1795. }
  1796. long_tmp = 1L;
  1797. if (long_count < 1)
  1798. status = -ERANGE;
  1799. else {
  1800. count = long_count;
  1801. for (i = 0; i < count; ++i) {
  1802. if (mask[mask_index >> 3] &
  1803. (1 << (mask_index & 7))) {
  1804. a = source1[index1 >> 3] &
  1805. (1 << (index1 & 7))
  1806. ? 1 : 0;
  1807. b = source2[index2 >> 3] &
  1808. (1 << (index2 & 7))
  1809. ? 1 : 0;
  1810. if (a != b) /* failure */
  1811. long_tmp = 0L;
  1812. }
  1813. ++index1;
  1814. ++index2;
  1815. ++mask_index;
  1816. }
  1817. }
  1818. stack[stack_ptr++] = long_tmp;
  1819. break;
  1820. }
  1821. default:
  1822. /* Unrecognized opcode -- ERROR! */
  1823. bad_opcode = 1;
  1824. break;
  1825. }
  1826. if (bad_opcode)
  1827. status = -ENOSYS;
  1828. if ((stack_ptr < 0) || (stack_ptr >= ALTERA_STACK_SIZE))
  1829. status = -EOVERFLOW;
  1830. if (status != 0) {
  1831. done = 1;
  1832. *error_address = (s32)(opcode_address - code_sect);
  1833. }
  1834. }
  1835. altera_free_buffers(astate);
  1836. /* Free all dynamically allocated arrays */
  1837. if ((attrs != NULL) && (vars != NULL))
  1838. for (i = 0; i < sym_count; ++i)
  1839. if (attrs[i] & 0x80)
  1840. kfree((void *)vars[i]);
  1841. kfree(vars);
  1842. kfree(var_size);
  1843. kfree(attrs);
  1844. kfree(proc_attributes);
  1845. return status;
  1846. }
  1847. static int altera_get_note(u8 *p, s32 program_size, s32 *offset,
  1848. char *key, char *value, int keylen, int vallen)
  1849. /*
  1850. * Gets key and value of NOTE fields in the JBC file.
  1851. * Can be called in two modes: if offset pointer is NULL,
  1852. * then the function searches for note fields which match
  1853. * the key string provided. If offset is not NULL, then
  1854. * the function finds the next note field of any key,
  1855. * starting at the offset specified by the offset pointer.
  1856. * Returns 0 for success, else appropriate error code
  1857. */
  1858. {
  1859. int status = -ENODATA;
  1860. u32 note_strings = 0L;
  1861. u32 note_table = 0L;
  1862. u32 note_count = 0L;
  1863. u32 first_word = 0L;
  1864. int version = 0;
  1865. int delta = 0;
  1866. char *key_ptr;
  1867. char *value_ptr;
  1868. int i;
  1869. /* Read header information */
  1870. if (program_size > 52L) {
  1871. first_word = get_unaligned_be32(&p[0]);
  1872. version = (first_word & 1L);
  1873. delta = version * 8;
  1874. note_strings = get_unaligned_be32(&p[8 + delta]);
  1875. note_table = get_unaligned_be32(&p[12 + delta]);
  1876. note_count = get_unaligned_be32(&p[44 + (2 * delta)]);
  1877. }
  1878. if ((first_word != 0x4A414D00L) && (first_word != 0x4A414D01L))
  1879. return -EIO;
  1880. if (note_count <= 0L)
  1881. return status;
  1882. if (offset == NULL) {
  1883. /*
  1884. * We will search for the first note with a specific key,
  1885. * and return only the value
  1886. */
  1887. for (i = 0; (i < note_count) &&
  1888. (status != 0); ++i) {
  1889. key_ptr = &p[note_strings +
  1890. get_unaligned_be32(
  1891. &p[note_table + (8 * i)])];
  1892. if (key && !strncasecmp(key, key_ptr, strlen(key_ptr))) {
  1893. status = 0;
  1894. value_ptr = &p[note_strings +
  1895. get_unaligned_be32(
  1896. &p[note_table + (8 * i) + 4])];
  1897. if (value != NULL)
  1898. strscpy(value, value_ptr, vallen);
  1899. }
  1900. }
  1901. } else {
  1902. /*
  1903. * We will search for the next note, regardless of the key,
  1904. * and return both the value and the key
  1905. */
  1906. i = *offset;
  1907. if ((i >= 0) && (i < note_count)) {
  1908. status = 0;
  1909. if (key != NULL)
  1910. strscpy(key, &p[note_strings +
  1911. get_unaligned_be32(
  1912. &p[note_table + (8 * i)])],
  1913. keylen);
  1914. if (value != NULL)
  1915. strscpy(value, &p[note_strings +
  1916. get_unaligned_be32(
  1917. &p[note_table + (8 * i) + 4])],
  1918. vallen);
  1919. *offset = i + 1;
  1920. }
  1921. }
  1922. return status;
  1923. }
  1924. static int altera_check_crc(u8 *p, s32 program_size)
  1925. {
  1926. int status = 0;
  1927. u16 local_expected = 0,
  1928. local_actual = 0,
  1929. shift_reg = 0xffff;
  1930. int bit, feedback;
  1931. u8 databyte;
  1932. u32 i;
  1933. u32 crc_section = 0L;
  1934. u32 first_word = 0L;
  1935. int version = 0;
  1936. int delta = 0;
  1937. if (program_size > 52L) {
  1938. first_word = get_unaligned_be32(&p[0]);
  1939. version = (first_word & 1L);
  1940. delta = version * 8;
  1941. crc_section = get_unaligned_be32(&p[32 + delta]);
  1942. }
  1943. if ((first_word != 0x4A414D00L) && (first_word != 0x4A414D01L))
  1944. status = -EIO;
  1945. if (crc_section >= program_size)
  1946. status = -EIO;
  1947. if (status == 0) {
  1948. local_expected = (u16)get_unaligned_be16(&p[crc_section]);
  1949. for (i = 0; i < crc_section; ++i) {
  1950. databyte = p[i];
  1951. for (bit = 0; bit < 8; bit++) {
  1952. feedback = (databyte ^ shift_reg) & 0x01;
  1953. shift_reg >>= 1;
  1954. if (feedback)
  1955. shift_reg ^= 0x8408;
  1956. databyte >>= 1;
  1957. }
  1958. }
  1959. local_actual = (u16)~shift_reg;
  1960. if (local_expected != local_actual)
  1961. status = -EILSEQ;
  1962. }
  1963. if (debug || status) {
  1964. switch (status) {
  1965. case 0:
  1966. printk(KERN_INFO "%s: CRC matched: %04x\n", __func__,
  1967. local_actual);
  1968. break;
  1969. case -EILSEQ:
  1970. printk(KERN_ERR "%s: CRC mismatch: expected %04x, "
  1971. "actual %04x\n", __func__, local_expected,
  1972. local_actual);
  1973. break;
  1974. case -EIO:
  1975. printk(KERN_ERR "%s: error: format isn't "
  1976. "recognized.\n", __func__);
  1977. break;
  1978. default:
  1979. printk(KERN_ERR "%s: CRC function returned error "
  1980. "code %d\n", __func__, status);
  1981. break;
  1982. }
  1983. }
  1984. return status;
  1985. }
  1986. static int altera_get_file_info(u8 *p,
  1987. s32 program_size,
  1988. int *format_version,
  1989. int *action_count,
  1990. int *procedure_count)
  1991. {
  1992. int status = -EIO;
  1993. u32 first_word = 0;
  1994. int version = 0;
  1995. if (program_size <= 52L)
  1996. return status;
  1997. first_word = get_unaligned_be32(&p[0]);
  1998. if ((first_word == 0x4A414D00L) || (first_word == 0x4A414D01L)) {
  1999. status = 0;
  2000. version = (first_word & 1L);
  2001. *format_version = version + 1;
  2002. if (version > 0) {
  2003. *action_count = get_unaligned_be32(&p[48]);
  2004. *procedure_count = get_unaligned_be32(&p[52]);
  2005. }
  2006. }
  2007. return status;
  2008. }
  2009. static int altera_get_act_info(u8 *p,
  2010. s32 program_size,
  2011. int index,
  2012. char **name,
  2013. char **description,
  2014. struct altera_procinfo **proc_list)
  2015. {
  2016. int status = -EIO;
  2017. struct altera_procinfo *procptr = NULL;
  2018. struct altera_procinfo *tmpptr = NULL;
  2019. u32 first_word = 0L;
  2020. u32 action_table = 0L;
  2021. u32 proc_table = 0L;
  2022. u32 str_table = 0L;
  2023. u32 note_strings = 0L;
  2024. u32 action_count = 0L;
  2025. u32 proc_count = 0L;
  2026. u32 act_name_id = 0L;
  2027. u32 act_desc_id = 0L;
  2028. u32 act_proc_id = 0L;
  2029. u32 act_proc_name = 0L;
  2030. u8 act_proc_attribute = 0;
  2031. if (program_size <= 52L)
  2032. return status;
  2033. /* Read header information */
  2034. first_word = get_unaligned_be32(&p[0]);
  2035. if (first_word != 0x4A414D01L)
  2036. return status;
  2037. action_table = get_unaligned_be32(&p[4]);
  2038. proc_table = get_unaligned_be32(&p[8]);
  2039. str_table = get_unaligned_be32(&p[12]);
  2040. note_strings = get_unaligned_be32(&p[16]);
  2041. action_count = get_unaligned_be32(&p[48]);
  2042. proc_count = get_unaligned_be32(&p[52]);
  2043. if (index >= action_count)
  2044. return status;
  2045. act_name_id = get_unaligned_be32(&p[action_table + (12 * index)]);
  2046. act_desc_id = get_unaligned_be32(&p[action_table + (12 * index) + 4]);
  2047. act_proc_id = get_unaligned_be32(&p[action_table + (12 * index) + 8]);
  2048. *name = &p[str_table + act_name_id];
  2049. if (act_desc_id < (note_strings - str_table))
  2050. *description = &p[str_table + act_desc_id];
  2051. do {
  2052. act_proc_name = get_unaligned_be32(
  2053. &p[proc_table + (13 * act_proc_id)]);
  2054. act_proc_attribute =
  2055. (p[proc_table + (13 * act_proc_id) + 8] & 0x03);
  2056. procptr =
  2057. kzalloc_obj(struct altera_procinfo);
  2058. if (procptr == NULL)
  2059. status = -ENOMEM;
  2060. else {
  2061. procptr->name = &p[str_table + act_proc_name];
  2062. procptr->attrs = act_proc_attribute;
  2063. procptr->next = NULL;
  2064. /* add record to end of linked list */
  2065. if (*proc_list == NULL)
  2066. *proc_list = procptr;
  2067. else {
  2068. tmpptr = *proc_list;
  2069. while (tmpptr->next != NULL)
  2070. tmpptr = tmpptr->next;
  2071. tmpptr->next = procptr;
  2072. }
  2073. }
  2074. act_proc_id = get_unaligned_be32(
  2075. &p[proc_table + (13 * act_proc_id) + 4]);
  2076. } while ((act_proc_id != 0) && (act_proc_id < proc_count));
  2077. return status;
  2078. }
  2079. int altera_init(struct altera_config *config, const struct firmware *fw)
  2080. {
  2081. struct altera_state *astate = NULL;
  2082. struct altera_procinfo *proc_list = NULL;
  2083. struct altera_procinfo *procptr = NULL;
  2084. char *key = NULL;
  2085. char *value = NULL;
  2086. char *action_name = NULL;
  2087. char *description = NULL;
  2088. int exec_result = 0;
  2089. int exit_code = 0;
  2090. int format_version = 0;
  2091. int action_count = 0;
  2092. int procedure_count = 0;
  2093. int index = 0;
  2094. s32 offset = 0L;
  2095. s32 error_address = 0L;
  2096. int retval = 0;
  2097. key = kzalloc(33, GFP_KERNEL);
  2098. if (!key) {
  2099. retval = -ENOMEM;
  2100. goto out;
  2101. }
  2102. value = kzalloc(257, GFP_KERNEL);
  2103. if (!value) {
  2104. retval = -ENOMEM;
  2105. goto free_key;
  2106. }
  2107. astate = kzalloc_obj(struct altera_state);
  2108. if (!astate) {
  2109. retval = -ENOMEM;
  2110. goto free_value;
  2111. }
  2112. astate->config = config;
  2113. if (!astate->config->jtag_io) {
  2114. if (!IS_ENABLED(CONFIG_HAS_IOPORT)) {
  2115. retval = -ENODEV;
  2116. goto free_state;
  2117. }
  2118. dprintk("%s: using byteblaster!\n", __func__);
  2119. astate->config->jtag_io = netup_jtag_io_lpt;
  2120. }
  2121. altera_check_crc((u8 *)fw->data, fw->size);
  2122. if (debug) {
  2123. altera_get_file_info((u8 *)fw->data, fw->size, &format_version,
  2124. &action_count, &procedure_count);
  2125. printk(KERN_INFO "%s: File format is %s ByteCode format\n",
  2126. __func__, (format_version == 2) ? "Jam STAPL" :
  2127. "pre-standardized Jam 1.1");
  2128. while (altera_get_note((u8 *)fw->data, fw->size,
  2129. &offset, key, value, 32, 256) == 0)
  2130. printk(KERN_INFO "%s: NOTE \"%s\" = \"%s\"\n",
  2131. __func__, key, value);
  2132. }
  2133. if (debug && (format_version == 2) && (action_count > 0)) {
  2134. printk(KERN_INFO "%s: Actions available:\n", __func__);
  2135. for (index = 0; index < action_count; ++index) {
  2136. altera_get_act_info((u8 *)fw->data, fw->size,
  2137. index, &action_name,
  2138. &description,
  2139. &proc_list);
  2140. if (description == NULL)
  2141. printk(KERN_INFO "%s: %s\n",
  2142. __func__,
  2143. action_name);
  2144. else
  2145. printk(KERN_INFO "%s: %s \"%s\"\n",
  2146. __func__,
  2147. action_name,
  2148. description);
  2149. procptr = proc_list;
  2150. while (procptr != NULL) {
  2151. if (procptr->attrs != 0)
  2152. printk(KERN_INFO "%s: %s (%s)\n",
  2153. __func__,
  2154. procptr->name,
  2155. (procptr->attrs == 1) ?
  2156. "optional" : "recommended");
  2157. proc_list = procptr->next;
  2158. kfree(procptr);
  2159. procptr = proc_list;
  2160. }
  2161. }
  2162. printk(KERN_INFO "\n");
  2163. }
  2164. exec_result = altera_execute(astate, (u8 *)fw->data, fw->size,
  2165. &error_address, &exit_code, &format_version);
  2166. if (exit_code)
  2167. exec_result = -EREMOTEIO;
  2168. if ((format_version == 2) && (exec_result == -EINVAL)) {
  2169. if (astate->config->action == NULL)
  2170. printk(KERN_ERR "%s: error: no action specified for "
  2171. "Jam STAPL file.\nprogram terminated.\n",
  2172. __func__);
  2173. else
  2174. printk(KERN_ERR "%s: error: action \"%s\""
  2175. " is not supported "
  2176. "for this Jam STAPL file.\n"
  2177. "Program terminated.\n", __func__,
  2178. astate->config->action);
  2179. } else if (exec_result)
  2180. printk(KERN_ERR "%s: error %d\n", __func__, exec_result);
  2181. free_state:
  2182. kfree(astate);
  2183. free_value:
  2184. kfree(value);
  2185. free_key:
  2186. kfree(key);
  2187. out:
  2188. return retval;
  2189. }
  2190. EXPORT_SYMBOL(altera_init);