altera.c 55 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497
  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 = kcalloc(sym_count, sizeof(long), GFP_KERNEL);
  267. if (vars == NULL)
  268. status = -ENOMEM;
  269. if (status == 0) {
  270. var_size = kcalloc(sym_count, sizeof(s32), GFP_KERNEL);
  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 = kcalloc(count, sizeof(long),
  1003. GFP_KERNEL);
  1004. vars[variable_id] = (long)longptr_tmp;
  1005. if (vars[variable_id] == 0) {
  1006. status = -ENOMEM;
  1007. break;
  1008. }
  1009. /* copy previous contents into buffer */
  1010. for (i = 0; i < count; ++i) {
  1011. longptr_tmp[i] =
  1012. get_unaligned_be32(&p[long_tmp]);
  1013. long_tmp += sizeof(long);
  1014. }
  1015. /*
  1016. * set bit 7 - buffer was
  1017. * dynamically allocated
  1018. */
  1019. attrs[variable_id] |= 0x80;
  1020. /* clear bit 2 - variable is writable */
  1021. attrs[variable_id] &= ~0x04;
  1022. attrs[variable_id] |= 0x01;
  1023. }
  1024. /* check that variable is a writable integer array */
  1025. if ((attrs[variable_id] & 0x1c) != 0x18)
  1026. status = -ERANGE;
  1027. else {
  1028. longptr_tmp = (long *)vars[variable_id];
  1029. /* pop the array index */
  1030. index = stack[--stack_ptr];
  1031. /* pop the value and store it into the array */
  1032. longptr_tmp[index] = stack[--stack_ptr];
  1033. }
  1034. break;
  1035. case OP_POPA:
  1036. /*
  1037. * Pop stack into Boolean array
  1038. * ...argument 0 is variable ID
  1039. * ...stack 0 is count
  1040. * ...stack 1 is array index
  1041. * ...stack 2 is value
  1042. */
  1043. if (!altera_check_stack(stack_ptr, 3, &status))
  1044. break;
  1045. variable_id = args[0];
  1046. /*
  1047. * If variable is read-only,
  1048. * convert to writable array
  1049. */
  1050. if ((version > 0) &&
  1051. ((attrs[variable_id] & 0x9c) == 0x0c)) {
  1052. /* Allocate a writable buffer for this array */
  1053. long_tmp =
  1054. (var_size[variable_id] + 7L) >> 3L;
  1055. charptr_tmp2 = (u8 *)vars[variable_id];
  1056. charptr_tmp =
  1057. kzalloc(long_tmp, GFP_KERNEL);
  1058. vars[variable_id] = (long)charptr_tmp;
  1059. if (vars[variable_id] == 0) {
  1060. status = -ENOMEM;
  1061. break;
  1062. }
  1063. /* zero the buffer */
  1064. for (long_idx = 0L;
  1065. long_idx < long_tmp;
  1066. ++long_idx) {
  1067. charptr_tmp[long_idx] = 0;
  1068. }
  1069. /* copy previous contents into buffer */
  1070. for (long_idx = 0L;
  1071. long_idx < var_size[variable_id];
  1072. ++long_idx) {
  1073. long_idx2 = long_idx;
  1074. if (charptr_tmp2[long_idx2 >> 3] &
  1075. (1 << (long_idx2 & 7))) {
  1076. charptr_tmp[long_idx >> 3] |=
  1077. (1 << (long_idx & 7));
  1078. }
  1079. }
  1080. /*
  1081. * set bit 7 - buffer was
  1082. * dynamically allocated
  1083. */
  1084. attrs[variable_id] |= 0x80;
  1085. /* clear bit 2 - variable is writable */
  1086. attrs[variable_id] &= ~0x04;
  1087. attrs[variable_id] |= 0x01;
  1088. }
  1089. /*
  1090. * check that variable is
  1091. * a writable Boolean array
  1092. */
  1093. if ((attrs[variable_id] & 0x1c) != 0x08) {
  1094. status = -ERANGE;
  1095. break;
  1096. }
  1097. charptr_tmp = (u8 *)vars[variable_id];
  1098. /* pop the count (number of bits to copy) */
  1099. long_count = stack[--stack_ptr];
  1100. /* pop the array index */
  1101. long_idx = stack[--stack_ptr];
  1102. reverse = 0;
  1103. if (version > 0) {
  1104. /*
  1105. * stack 0 = array right index
  1106. * stack 1 = array left index
  1107. */
  1108. if (long_idx > long_count) {
  1109. reverse = 1;
  1110. long_tmp = long_count;
  1111. long_count = 1 + long_idx -
  1112. long_count;
  1113. long_idx = long_tmp;
  1114. /* reverse POPA is not supported */
  1115. status = -ERANGE;
  1116. break;
  1117. } else
  1118. long_count = 1 + long_count -
  1119. long_idx;
  1120. }
  1121. /* pop the data */
  1122. long_tmp = stack[--stack_ptr];
  1123. if (long_count < 1) {
  1124. status = -ERANGE;
  1125. break;
  1126. }
  1127. for (i = 0; i < long_count; ++i) {
  1128. if (long_tmp & (1L << (s32) i))
  1129. charptr_tmp[long_idx >> 3L] |=
  1130. (1L << (long_idx & 7L));
  1131. else
  1132. charptr_tmp[long_idx >> 3L] &=
  1133. ~(1L << (long_idx & 7L));
  1134. ++long_idx;
  1135. }
  1136. break;
  1137. case OP_JMPZ:
  1138. /*
  1139. * Pop stack and branch if zero
  1140. * ...argument 0 is address
  1141. * ...stack 0 is condition value
  1142. */
  1143. if (altera_check_stack(stack_ptr, 1, &status)) {
  1144. if (stack[--stack_ptr] == 0) {
  1145. pc = args[0] + code_sect;
  1146. if ((pc < code_sect) ||
  1147. (pc >= debug_sect))
  1148. status = -ERANGE;
  1149. }
  1150. }
  1151. break;
  1152. case OP_DS:
  1153. case OP_IS:
  1154. /*
  1155. * DRSCAN
  1156. * IRSCAN
  1157. * ...argument 0 is scan data variable ID
  1158. * ...stack 0 is array index
  1159. * ...stack 1 is count
  1160. */
  1161. if (!altera_check_stack(stack_ptr, 2, &status))
  1162. break;
  1163. long_idx = stack[--stack_ptr];
  1164. long_count = stack[--stack_ptr];
  1165. reverse = 0;
  1166. if (version > 0) {
  1167. /*
  1168. * stack 0 = array right index
  1169. * stack 1 = array left index
  1170. * stack 2 = count
  1171. */
  1172. long_tmp = long_count;
  1173. long_count = stack[--stack_ptr];
  1174. if (long_idx > long_tmp) {
  1175. reverse = 1;
  1176. long_idx = long_tmp;
  1177. }
  1178. }
  1179. charptr_tmp = (u8 *)vars[args[0]];
  1180. if (reverse) {
  1181. /*
  1182. * allocate a buffer
  1183. * and reverse the data order
  1184. */
  1185. charptr_tmp2 = charptr_tmp;
  1186. charptr_tmp = kzalloc((long_count >> 3) + 1,
  1187. GFP_KERNEL);
  1188. if (charptr_tmp == NULL) {
  1189. status = -ENOMEM;
  1190. break;
  1191. }
  1192. long_tmp = long_idx + long_count - 1;
  1193. long_idx2 = 0;
  1194. while (long_idx2 < long_count) {
  1195. if (charptr_tmp2[long_tmp >> 3] &
  1196. (1 << (long_tmp & 7)))
  1197. charptr_tmp[long_idx2 >> 3] |=
  1198. (1 << (long_idx2 & 7));
  1199. else
  1200. charptr_tmp[long_idx2 >> 3] &=
  1201. ~(1 << (long_idx2 & 7));
  1202. --long_tmp;
  1203. ++long_idx2;
  1204. }
  1205. }
  1206. if (opcode == 0x51) /* DS */
  1207. status = altera_drscan(astate, long_count,
  1208. charptr_tmp, long_idx);
  1209. else /* IS */
  1210. status = altera_irscan(astate, long_count,
  1211. charptr_tmp, long_idx);
  1212. if (reverse)
  1213. kfree(charptr_tmp);
  1214. break;
  1215. case OP_DPRA:
  1216. /*
  1217. * DRPRE with array data
  1218. * ...argument 0 is variable ID
  1219. * ...stack 0 is array index
  1220. * ...stack 1 is count
  1221. */
  1222. if (!altera_check_stack(stack_ptr, 2, &status))
  1223. break;
  1224. index = stack[--stack_ptr];
  1225. count = stack[--stack_ptr];
  1226. if (version > 0)
  1227. /*
  1228. * stack 0 = array right index
  1229. * stack 1 = array left index
  1230. */
  1231. count = 1 + count - index;
  1232. charptr_tmp = (u8 *)vars[args[0]];
  1233. status = altera_set_dr_pre(&astate->js, count, index,
  1234. charptr_tmp);
  1235. break;
  1236. case OP_DPOA:
  1237. /*
  1238. * DRPOST with array data
  1239. * ...argument 0 is variable ID
  1240. * ...stack 0 is array index
  1241. * ...stack 1 is count
  1242. */
  1243. if (!altera_check_stack(stack_ptr, 2, &status))
  1244. break;
  1245. index = stack[--stack_ptr];
  1246. count = stack[--stack_ptr];
  1247. if (version > 0)
  1248. /*
  1249. * stack 0 = array right index
  1250. * stack 1 = array left index
  1251. */
  1252. count = 1 + count - index;
  1253. charptr_tmp = (u8 *)vars[args[0]];
  1254. status = altera_set_dr_post(&astate->js, count, index,
  1255. charptr_tmp);
  1256. break;
  1257. case OP_IPRA:
  1258. /*
  1259. * IRPRE with array data
  1260. * ...argument 0 is variable ID
  1261. * ...stack 0 is array index
  1262. * ...stack 1 is count
  1263. */
  1264. if (!altera_check_stack(stack_ptr, 2, &status))
  1265. break;
  1266. index = stack[--stack_ptr];
  1267. count = stack[--stack_ptr];
  1268. if (version > 0)
  1269. /*
  1270. * stack 0 = array right index
  1271. * stack 1 = array left index
  1272. */
  1273. count = 1 + count - index;
  1274. charptr_tmp = (u8 *)vars[args[0]];
  1275. status = altera_set_ir_pre(&astate->js, count, index,
  1276. charptr_tmp);
  1277. break;
  1278. case OP_IPOA:
  1279. /*
  1280. * IRPOST with array data
  1281. * ...argument 0 is variable ID
  1282. * ...stack 0 is array index
  1283. * ...stack 1 is count
  1284. */
  1285. if (!altera_check_stack(stack_ptr, 2, &status))
  1286. break;
  1287. index = stack[--stack_ptr];
  1288. count = stack[--stack_ptr];
  1289. if (version > 0)
  1290. /*
  1291. * stack 0 = array right index
  1292. * stack 1 = array left index
  1293. */
  1294. count = 1 + count - index;
  1295. charptr_tmp = (u8 *)vars[args[0]];
  1296. status = altera_set_ir_post(&astate->js, count, index,
  1297. charptr_tmp);
  1298. break;
  1299. case OP_EXPT:
  1300. /*
  1301. * EXPORT
  1302. * ...argument 0 is string ID
  1303. * ...stack 0 is integer expression
  1304. */
  1305. if (altera_check_stack(stack_ptr, 1, &status)) {
  1306. name = &p[str_table + args[0]];
  1307. long_tmp = stack[--stack_ptr];
  1308. altera_export_int(name, long_tmp);
  1309. }
  1310. break;
  1311. case OP_PSHE:
  1312. /*
  1313. * Push integer array element
  1314. * ...argument 0 is variable ID
  1315. * ...stack 0 is array index
  1316. */
  1317. if (!altera_check_stack(stack_ptr, 1, &status))
  1318. break;
  1319. variable_id = args[0];
  1320. index = stack[stack_ptr - 1];
  1321. /* check variable type */
  1322. if ((attrs[variable_id] & 0x1f) == 0x19) {
  1323. /* writable integer array */
  1324. longptr_tmp = (long *)vars[variable_id];
  1325. stack[stack_ptr - 1] = longptr_tmp[index];
  1326. } else if ((attrs[variable_id] & 0x1f) == 0x1c) {
  1327. /* read-only integer array */
  1328. long_tmp = vars[variable_id] +
  1329. (index * sizeof(long));
  1330. stack[stack_ptr - 1] =
  1331. get_unaligned_be32(&p[long_tmp]);
  1332. } else
  1333. status = -ERANGE;
  1334. break;
  1335. case OP_PSHA:
  1336. /*
  1337. * Push Boolean array
  1338. * ...argument 0 is variable ID
  1339. * ...stack 0 is count
  1340. * ...stack 1 is array index
  1341. */
  1342. if (!altera_check_stack(stack_ptr, 2, &status))
  1343. break;
  1344. variable_id = args[0];
  1345. /* check that variable is a Boolean array */
  1346. if ((attrs[variable_id] & 0x18) != 0x08) {
  1347. status = -ERANGE;
  1348. break;
  1349. }
  1350. charptr_tmp = (u8 *)vars[variable_id];
  1351. /* pop the count (number of bits to copy) */
  1352. count = stack[--stack_ptr];
  1353. /* pop the array index */
  1354. index = stack[stack_ptr - 1];
  1355. if (version > 0)
  1356. /*
  1357. * stack 0 = array right index
  1358. * stack 1 = array left index
  1359. */
  1360. count = 1 + count - index;
  1361. if ((count < 1) || (count > 32)) {
  1362. status = -ERANGE;
  1363. break;
  1364. }
  1365. long_tmp = 0L;
  1366. for (i = 0; i < count; ++i)
  1367. if (charptr_tmp[(i + index) >> 3] &
  1368. (1 << ((i + index) & 7)))
  1369. long_tmp |= (1L << i);
  1370. stack[stack_ptr - 1] = long_tmp;
  1371. break;
  1372. case OP_DYNA:
  1373. /*
  1374. * Dynamically change size of array
  1375. * ...argument 0 is variable ID
  1376. * ...stack 0 is new size
  1377. */
  1378. if (!altera_check_stack(stack_ptr, 1, &status))
  1379. break;
  1380. variable_id = args[0];
  1381. long_tmp = stack[--stack_ptr];
  1382. if (long_tmp > var_size[variable_id]) {
  1383. var_size[variable_id] = long_tmp;
  1384. if (attrs[variable_id] & 0x10)
  1385. /* allocate integer array */
  1386. long_tmp *= sizeof(long);
  1387. else
  1388. /* allocate Boolean array */
  1389. long_tmp = (long_tmp + 7) >> 3;
  1390. /*
  1391. * If the buffer was previously allocated,
  1392. * free it
  1393. */
  1394. if (attrs[variable_id] & 0x80) {
  1395. kfree((void *)vars[variable_id]);
  1396. vars[variable_id] = 0;
  1397. }
  1398. /*
  1399. * Allocate a new buffer
  1400. * of the requested size
  1401. */
  1402. vars[variable_id] = (long)
  1403. kzalloc(long_tmp, GFP_KERNEL);
  1404. if (vars[variable_id] == 0) {
  1405. status = -ENOMEM;
  1406. break;
  1407. }
  1408. /*
  1409. * Set the attribute bit to indicate that
  1410. * this buffer was dynamically allocated and
  1411. * should be freed later
  1412. */
  1413. attrs[variable_id] |= 0x80;
  1414. /* zero out memory */
  1415. count = ((var_size[variable_id] + 7L) /
  1416. 8L);
  1417. charptr_tmp = (u8 *)(vars[variable_id]);
  1418. for (index = 0; index < count; ++index)
  1419. charptr_tmp[index] = 0;
  1420. }
  1421. break;
  1422. case OP_EXPV:
  1423. /*
  1424. * Export Boolean array
  1425. * ...argument 0 is string ID
  1426. * ...stack 0 is variable ID
  1427. * ...stack 1 is array right index
  1428. * ...stack 2 is array left index
  1429. */
  1430. if (!altera_check_stack(stack_ptr, 3, &status))
  1431. break;
  1432. if (version == 0) {
  1433. /* EXPV is not supported in JBC 1.0 */
  1434. bad_opcode = 1;
  1435. break;
  1436. }
  1437. name = &p[str_table + args[0]];
  1438. variable_id = stack[--stack_ptr];
  1439. long_idx = stack[--stack_ptr];/* right indx */
  1440. long_idx2 = stack[--stack_ptr];/* left indx */
  1441. if (long_idx > long_idx2) {
  1442. /* reverse indices not supported */
  1443. status = -ERANGE;
  1444. break;
  1445. }
  1446. long_count = 1 + long_idx2 - long_idx;
  1447. charptr_tmp = (u8 *)vars[variable_id];
  1448. charptr_tmp2 = NULL;
  1449. if ((long_idx & 7L) != 0) {
  1450. s32 k = long_idx;
  1451. charptr_tmp2 =
  1452. kzalloc(((long_count + 7L) / 8L),
  1453. GFP_KERNEL);
  1454. if (charptr_tmp2 == NULL) {
  1455. status = -ENOMEM;
  1456. break;
  1457. }
  1458. for (i = 0; i < long_count; ++i) {
  1459. if (charptr_tmp[k >> 3] &
  1460. (1 << (k & 7)))
  1461. charptr_tmp2[i >> 3] |=
  1462. (1 << (i & 7));
  1463. else
  1464. charptr_tmp2[i >> 3] &=
  1465. ~(1 << (i & 7));
  1466. ++k;
  1467. }
  1468. charptr_tmp = charptr_tmp2;
  1469. } else if (long_idx != 0)
  1470. charptr_tmp = &charptr_tmp[long_idx >> 3];
  1471. altera_export_bool_array(name, charptr_tmp,
  1472. long_count);
  1473. /* free allocated buffer */
  1474. if ((long_idx & 7L) != 0)
  1475. kfree(charptr_tmp2);
  1476. break;
  1477. case OP_COPY: {
  1478. /*
  1479. * Array copy
  1480. * ...argument 0 is dest ID
  1481. * ...argument 1 is source ID
  1482. * ...stack 0 is count
  1483. * ...stack 1 is dest index
  1484. * ...stack 2 is source index
  1485. */
  1486. s32 copy_count;
  1487. s32 copy_index;
  1488. s32 copy_index2;
  1489. s32 destleft;
  1490. s32 src_count;
  1491. s32 dest_count;
  1492. int src_reverse = 0;
  1493. int dest_reverse = 0;
  1494. if (!altera_check_stack(stack_ptr, 3, &status))
  1495. break;
  1496. copy_count = stack[--stack_ptr];
  1497. copy_index = stack[--stack_ptr];
  1498. copy_index2 = stack[--stack_ptr];
  1499. reverse = 0;
  1500. if (version > 0) {
  1501. /*
  1502. * stack 0 = source right index
  1503. * stack 1 = source left index
  1504. * stack 2 = destination right index
  1505. * stack 3 = destination left index
  1506. */
  1507. destleft = stack[--stack_ptr];
  1508. if (copy_count > copy_index) {
  1509. src_reverse = 1;
  1510. reverse = 1;
  1511. src_count = 1 + copy_count - copy_index;
  1512. /* copy_index = source start index */
  1513. } else {
  1514. src_count = 1 + copy_index - copy_count;
  1515. /* source start index */
  1516. copy_index = copy_count;
  1517. }
  1518. if (copy_index2 > destleft) {
  1519. dest_reverse = 1;
  1520. reverse = !reverse;
  1521. dest_count = 1 + copy_index2 - destleft;
  1522. /* destination start index */
  1523. copy_index2 = destleft;
  1524. } else
  1525. dest_count = 1 + destleft - copy_index2;
  1526. copy_count = (src_count < dest_count) ?
  1527. src_count : dest_count;
  1528. if ((src_reverse || dest_reverse) &&
  1529. (src_count != dest_count))
  1530. /*
  1531. * If either the source or destination
  1532. * is reversed, we can't tolerate
  1533. * a length mismatch, because we
  1534. * "left justify" arrays when copying.
  1535. * This won't work correctly
  1536. * with reversed arrays.
  1537. */
  1538. status = -ERANGE;
  1539. }
  1540. count = copy_count;
  1541. index = copy_index;
  1542. index2 = copy_index2;
  1543. /*
  1544. * If destination is a read-only array,
  1545. * allocate a buffer and convert it to a writable array
  1546. */
  1547. variable_id = args[1];
  1548. if ((version > 0) &&
  1549. ((attrs[variable_id] & 0x9c) == 0x0c)) {
  1550. /* Allocate a writable buffer for this array */
  1551. long_tmp =
  1552. (var_size[variable_id] + 7L) >> 3L;
  1553. charptr_tmp2 = (u8 *)vars[variable_id];
  1554. charptr_tmp =
  1555. kzalloc(long_tmp, GFP_KERNEL);
  1556. vars[variable_id] = (long)charptr_tmp;
  1557. if (vars[variable_id] == 0) {
  1558. status = -ENOMEM;
  1559. break;
  1560. }
  1561. /* zero the buffer */
  1562. for (long_idx = 0L; long_idx < long_tmp;
  1563. ++long_idx)
  1564. charptr_tmp[long_idx] = 0;
  1565. /* copy previous contents into buffer */
  1566. for (long_idx = 0L;
  1567. long_idx < var_size[variable_id];
  1568. ++long_idx) {
  1569. long_idx2 = long_idx;
  1570. if (charptr_tmp2[long_idx2 >> 3] &
  1571. (1 << (long_idx2 & 7)))
  1572. charptr_tmp[long_idx >> 3] |=
  1573. (1 << (long_idx & 7));
  1574. }
  1575. /*
  1576. set bit 7 - buffer was dynamically allocated */
  1577. attrs[variable_id] |= 0x80;
  1578. /* clear bit 2 - variable is writable */
  1579. attrs[variable_id] &= ~0x04;
  1580. attrs[variable_id] |= 0x01;
  1581. }
  1582. charptr_tmp = (u8 *)vars[args[1]];
  1583. charptr_tmp2 = (u8 *)vars[args[0]];
  1584. /* check if destination is a writable Boolean array */
  1585. if ((attrs[args[1]] & 0x1c) != 0x08) {
  1586. status = -ERANGE;
  1587. break;
  1588. }
  1589. if (count < 1) {
  1590. status = -ERANGE;
  1591. break;
  1592. }
  1593. if (reverse)
  1594. index2 += (count - 1);
  1595. for (i = 0; i < count; ++i) {
  1596. if (charptr_tmp2[index >> 3] &
  1597. (1 << (index & 7)))
  1598. charptr_tmp[index2 >> 3] |=
  1599. (1 << (index2 & 7));
  1600. else
  1601. charptr_tmp[index2 >> 3] &=
  1602. ~(1 << (index2 & 7));
  1603. ++index;
  1604. if (reverse)
  1605. --index2;
  1606. else
  1607. ++index2;
  1608. }
  1609. break;
  1610. }
  1611. case OP_DSC:
  1612. case OP_ISC: {
  1613. /*
  1614. * DRSCAN with capture
  1615. * IRSCAN with capture
  1616. * ...argument 0 is scan data variable ID
  1617. * ...argument 1 is capture variable ID
  1618. * ...stack 0 is capture index
  1619. * ...stack 1 is scan data index
  1620. * ...stack 2 is count
  1621. */
  1622. s32 scan_right, scan_left;
  1623. s32 capture_count = 0;
  1624. s32 scan_count = 0;
  1625. s32 capture_index;
  1626. s32 scan_index;
  1627. if (!altera_check_stack(stack_ptr, 3, &status))
  1628. break;
  1629. capture_index = stack[--stack_ptr];
  1630. scan_index = stack[--stack_ptr];
  1631. if (version > 0) {
  1632. /*
  1633. * stack 0 = capture right index
  1634. * stack 1 = capture left index
  1635. * stack 2 = scan right index
  1636. * stack 3 = scan left index
  1637. * stack 4 = count
  1638. */
  1639. scan_right = stack[--stack_ptr];
  1640. scan_left = stack[--stack_ptr];
  1641. capture_count = 1 + scan_index - capture_index;
  1642. scan_count = 1 + scan_left - scan_right;
  1643. scan_index = scan_right;
  1644. }
  1645. long_count = stack[--stack_ptr];
  1646. /*
  1647. * If capture array is read-only, allocate a buffer
  1648. * and convert it to a writable array
  1649. */
  1650. variable_id = args[1];
  1651. if ((version > 0) &&
  1652. ((attrs[variable_id] & 0x9c) == 0x0c)) {
  1653. /* Allocate a writable buffer for this array */
  1654. long_tmp =
  1655. (var_size[variable_id] + 7L) >> 3L;
  1656. charptr_tmp2 = (u8 *)vars[variable_id];
  1657. charptr_tmp =
  1658. kzalloc(long_tmp, GFP_KERNEL);
  1659. vars[variable_id] = (long)charptr_tmp;
  1660. if (vars[variable_id] == 0) {
  1661. status = -ENOMEM;
  1662. break;
  1663. }
  1664. /* zero the buffer */
  1665. for (long_idx = 0L; long_idx < long_tmp;
  1666. ++long_idx)
  1667. charptr_tmp[long_idx] = 0;
  1668. /* copy previous contents into buffer */
  1669. for (long_idx = 0L;
  1670. long_idx < var_size[variable_id];
  1671. ++long_idx) {
  1672. long_idx2 = long_idx;
  1673. if (charptr_tmp2[long_idx2 >> 3] &
  1674. (1 << (long_idx2 & 7)))
  1675. charptr_tmp[long_idx >> 3] |=
  1676. (1 << (long_idx & 7));
  1677. }
  1678. /*
  1679. * set bit 7 - buffer was
  1680. * dynamically allocated
  1681. */
  1682. attrs[variable_id] |= 0x80;
  1683. /* clear bit 2 - variable is writable */
  1684. attrs[variable_id] &= ~0x04;
  1685. attrs[variable_id] |= 0x01;
  1686. }
  1687. charptr_tmp = (u8 *)vars[args[0]];
  1688. charptr_tmp2 = (u8 *)vars[args[1]];
  1689. if ((version > 0) &&
  1690. ((long_count > capture_count) ||
  1691. (long_count > scan_count))) {
  1692. status = -ERANGE;
  1693. break;
  1694. }
  1695. /*
  1696. * check that capture array
  1697. * is a writable Boolean array
  1698. */
  1699. if ((attrs[args[1]] & 0x1c) != 0x08) {
  1700. status = -ERANGE;
  1701. break;
  1702. }
  1703. if (status == 0) {
  1704. if (opcode == 0x82) /* DSC */
  1705. status = altera_swap_dr(astate,
  1706. long_count,
  1707. charptr_tmp,
  1708. scan_index,
  1709. charptr_tmp2,
  1710. capture_index);
  1711. else /* ISC */
  1712. status = altera_swap_ir(astate,
  1713. long_count,
  1714. charptr_tmp,
  1715. scan_index,
  1716. charptr_tmp2,
  1717. capture_index);
  1718. }
  1719. break;
  1720. }
  1721. case OP_WAIT:
  1722. /*
  1723. * WAIT
  1724. * ...argument 0 is wait state
  1725. * ...argument 1 is end state
  1726. * ...stack 0 is cycles
  1727. * ...stack 1 is microseconds
  1728. */
  1729. if (!altera_check_stack(stack_ptr, 2, &status))
  1730. break;
  1731. long_tmp = stack[--stack_ptr];
  1732. if (long_tmp != 0L)
  1733. status = altera_wait_cycles(astate, long_tmp,
  1734. args[0]);
  1735. long_tmp = stack[--stack_ptr];
  1736. if ((status == 0) && (long_tmp != 0L))
  1737. status = altera_wait_msecs(astate,
  1738. long_tmp,
  1739. args[0]);
  1740. if ((status == 0) && (args[1] != args[0]))
  1741. status = altera_goto_jstate(astate,
  1742. args[1]);
  1743. if (version > 0) {
  1744. --stack_ptr; /* throw away MAX cycles */
  1745. --stack_ptr; /* throw away MAX microseconds */
  1746. }
  1747. break;
  1748. case OP_CMPA: {
  1749. /*
  1750. * Array compare
  1751. * ...argument 0 is source 1 ID
  1752. * ...argument 1 is source 2 ID
  1753. * ...argument 2 is mask ID
  1754. * ...stack 0 is source 1 index
  1755. * ...stack 1 is source 2 index
  1756. * ...stack 2 is mask index
  1757. * ...stack 3 is count
  1758. */
  1759. s32 a, b;
  1760. u8 *source1 = (u8 *)vars[args[0]];
  1761. u8 *source2 = (u8 *)vars[args[1]];
  1762. u8 *mask = (u8 *)vars[args[2]];
  1763. u32 index1;
  1764. u32 index2;
  1765. u32 mask_index;
  1766. if (!altera_check_stack(stack_ptr, 4, &status))
  1767. break;
  1768. index1 = stack[--stack_ptr];
  1769. index2 = stack[--stack_ptr];
  1770. mask_index = stack[--stack_ptr];
  1771. long_count = stack[--stack_ptr];
  1772. if (version > 0) {
  1773. /*
  1774. * stack 0 = source 1 right index
  1775. * stack 1 = source 1 left index
  1776. * stack 2 = source 2 right index
  1777. * stack 3 = source 2 left index
  1778. * stack 4 = mask right index
  1779. * stack 5 = mask left index
  1780. */
  1781. s32 mask_right = stack[--stack_ptr];
  1782. s32 mask_left = stack[--stack_ptr];
  1783. /* source 1 count */
  1784. a = 1 + index2 - index1;
  1785. /* source 2 count */
  1786. b = 1 + long_count - mask_index;
  1787. a = (a < b) ? a : b;
  1788. /* mask count */
  1789. b = 1 + mask_left - mask_right;
  1790. a = (a < b) ? a : b;
  1791. /* source 2 start index */
  1792. index2 = mask_index;
  1793. /* mask start index */
  1794. mask_index = mask_right;
  1795. long_count = a;
  1796. }
  1797. long_tmp = 1L;
  1798. if (long_count < 1)
  1799. status = -ERANGE;
  1800. else {
  1801. count = long_count;
  1802. for (i = 0; i < count; ++i) {
  1803. if (mask[mask_index >> 3] &
  1804. (1 << (mask_index & 7))) {
  1805. a = source1[index1 >> 3] &
  1806. (1 << (index1 & 7))
  1807. ? 1 : 0;
  1808. b = source2[index2 >> 3] &
  1809. (1 << (index2 & 7))
  1810. ? 1 : 0;
  1811. if (a != b) /* failure */
  1812. long_tmp = 0L;
  1813. }
  1814. ++index1;
  1815. ++index2;
  1816. ++mask_index;
  1817. }
  1818. }
  1819. stack[stack_ptr++] = long_tmp;
  1820. break;
  1821. }
  1822. default:
  1823. /* Unrecognized opcode -- ERROR! */
  1824. bad_opcode = 1;
  1825. break;
  1826. }
  1827. if (bad_opcode)
  1828. status = -ENOSYS;
  1829. if ((stack_ptr < 0) || (stack_ptr >= ALTERA_STACK_SIZE))
  1830. status = -EOVERFLOW;
  1831. if (status != 0) {
  1832. done = 1;
  1833. *error_address = (s32)(opcode_address - code_sect);
  1834. }
  1835. }
  1836. altera_free_buffers(astate);
  1837. /* Free all dynamically allocated arrays */
  1838. if ((attrs != NULL) && (vars != NULL))
  1839. for (i = 0; i < sym_count; ++i)
  1840. if (attrs[i] & 0x80)
  1841. kfree((void *)vars[i]);
  1842. kfree(vars);
  1843. kfree(var_size);
  1844. kfree(attrs);
  1845. kfree(proc_attributes);
  1846. return status;
  1847. }
  1848. static int altera_get_note(u8 *p, s32 program_size, s32 *offset,
  1849. char *key, char *value, int keylen, int vallen)
  1850. /*
  1851. * Gets key and value of NOTE fields in the JBC file.
  1852. * Can be called in two modes: if offset pointer is NULL,
  1853. * then the function searches for note fields which match
  1854. * the key string provided. If offset is not NULL, then
  1855. * the function finds the next note field of any key,
  1856. * starting at the offset specified by the offset pointer.
  1857. * Returns 0 for success, else appropriate error code
  1858. */
  1859. {
  1860. int status = -ENODATA;
  1861. u32 note_strings = 0L;
  1862. u32 note_table = 0L;
  1863. u32 note_count = 0L;
  1864. u32 first_word = 0L;
  1865. int version = 0;
  1866. int delta = 0;
  1867. char *key_ptr;
  1868. char *value_ptr;
  1869. int i;
  1870. /* Read header information */
  1871. if (program_size > 52L) {
  1872. first_word = get_unaligned_be32(&p[0]);
  1873. version = (first_word & 1L);
  1874. delta = version * 8;
  1875. note_strings = get_unaligned_be32(&p[8 + delta]);
  1876. note_table = get_unaligned_be32(&p[12 + delta]);
  1877. note_count = get_unaligned_be32(&p[44 + (2 * delta)]);
  1878. }
  1879. if ((first_word != 0x4A414D00L) && (first_word != 0x4A414D01L))
  1880. return -EIO;
  1881. if (note_count <= 0L)
  1882. return status;
  1883. if (offset == NULL) {
  1884. /*
  1885. * We will search for the first note with a specific key,
  1886. * and return only the value
  1887. */
  1888. for (i = 0; (i < note_count) &&
  1889. (status != 0); ++i) {
  1890. key_ptr = &p[note_strings +
  1891. get_unaligned_be32(
  1892. &p[note_table + (8 * i)])];
  1893. if (key && !strncasecmp(key, key_ptr, strlen(key_ptr))) {
  1894. status = 0;
  1895. value_ptr = &p[note_strings +
  1896. get_unaligned_be32(
  1897. &p[note_table + (8 * i) + 4])];
  1898. if (value != NULL)
  1899. strscpy(value, value_ptr, vallen);
  1900. }
  1901. }
  1902. } else {
  1903. /*
  1904. * We will search for the next note, regardless of the key,
  1905. * and return both the value and the key
  1906. */
  1907. i = *offset;
  1908. if ((i >= 0) && (i < note_count)) {
  1909. status = 0;
  1910. if (key != NULL)
  1911. strscpy(key, &p[note_strings +
  1912. get_unaligned_be32(
  1913. &p[note_table + (8 * i)])],
  1914. keylen);
  1915. if (value != NULL)
  1916. strscpy(value, &p[note_strings +
  1917. get_unaligned_be32(
  1918. &p[note_table + (8 * i) + 4])],
  1919. vallen);
  1920. *offset = i + 1;
  1921. }
  1922. }
  1923. return status;
  1924. }
  1925. static int altera_check_crc(u8 *p, s32 program_size)
  1926. {
  1927. int status = 0;
  1928. u16 local_expected = 0,
  1929. local_actual = 0,
  1930. shift_reg = 0xffff;
  1931. int bit, feedback;
  1932. u8 databyte;
  1933. u32 i;
  1934. u32 crc_section = 0L;
  1935. u32 first_word = 0L;
  1936. int version = 0;
  1937. int delta = 0;
  1938. if (program_size > 52L) {
  1939. first_word = get_unaligned_be32(&p[0]);
  1940. version = (first_word & 1L);
  1941. delta = version * 8;
  1942. crc_section = get_unaligned_be32(&p[32 + delta]);
  1943. }
  1944. if ((first_word != 0x4A414D00L) && (first_word != 0x4A414D01L))
  1945. status = -EIO;
  1946. if (crc_section >= program_size)
  1947. status = -EIO;
  1948. if (status == 0) {
  1949. local_expected = (u16)get_unaligned_be16(&p[crc_section]);
  1950. for (i = 0; i < crc_section; ++i) {
  1951. databyte = p[i];
  1952. for (bit = 0; bit < 8; bit++) {
  1953. feedback = (databyte ^ shift_reg) & 0x01;
  1954. shift_reg >>= 1;
  1955. if (feedback)
  1956. shift_reg ^= 0x8408;
  1957. databyte >>= 1;
  1958. }
  1959. }
  1960. local_actual = (u16)~shift_reg;
  1961. if (local_expected != local_actual)
  1962. status = -EILSEQ;
  1963. }
  1964. if (debug || status) {
  1965. switch (status) {
  1966. case 0:
  1967. printk(KERN_INFO "%s: CRC matched: %04x\n", __func__,
  1968. local_actual);
  1969. break;
  1970. case -EILSEQ:
  1971. printk(KERN_ERR "%s: CRC mismatch: expected %04x, "
  1972. "actual %04x\n", __func__, local_expected,
  1973. local_actual);
  1974. break;
  1975. case -EIO:
  1976. printk(KERN_ERR "%s: error: format isn't "
  1977. "recognized.\n", __func__);
  1978. break;
  1979. default:
  1980. printk(KERN_ERR "%s: CRC function returned error "
  1981. "code %d\n", __func__, status);
  1982. break;
  1983. }
  1984. }
  1985. return status;
  1986. }
  1987. static int altera_get_file_info(u8 *p,
  1988. s32 program_size,
  1989. int *format_version,
  1990. int *action_count,
  1991. int *procedure_count)
  1992. {
  1993. int status = -EIO;
  1994. u32 first_word = 0;
  1995. int version = 0;
  1996. if (program_size <= 52L)
  1997. return status;
  1998. first_word = get_unaligned_be32(&p[0]);
  1999. if ((first_word == 0x4A414D00L) || (first_word == 0x4A414D01L)) {
  2000. status = 0;
  2001. version = (first_word & 1L);
  2002. *format_version = version + 1;
  2003. if (version > 0) {
  2004. *action_count = get_unaligned_be32(&p[48]);
  2005. *procedure_count = get_unaligned_be32(&p[52]);
  2006. }
  2007. }
  2008. return status;
  2009. }
  2010. static int altera_get_act_info(u8 *p,
  2011. s32 program_size,
  2012. int index,
  2013. char **name,
  2014. char **description,
  2015. struct altera_procinfo **proc_list)
  2016. {
  2017. int status = -EIO;
  2018. struct altera_procinfo *procptr = NULL;
  2019. struct altera_procinfo *tmpptr = NULL;
  2020. u32 first_word = 0L;
  2021. u32 action_table = 0L;
  2022. u32 proc_table = 0L;
  2023. u32 str_table = 0L;
  2024. u32 note_strings = 0L;
  2025. u32 action_count = 0L;
  2026. u32 proc_count = 0L;
  2027. u32 act_name_id = 0L;
  2028. u32 act_desc_id = 0L;
  2029. u32 act_proc_id = 0L;
  2030. u32 act_proc_name = 0L;
  2031. u8 act_proc_attribute = 0;
  2032. if (program_size <= 52L)
  2033. return status;
  2034. /* Read header information */
  2035. first_word = get_unaligned_be32(&p[0]);
  2036. if (first_word != 0x4A414D01L)
  2037. return status;
  2038. action_table = get_unaligned_be32(&p[4]);
  2039. proc_table = get_unaligned_be32(&p[8]);
  2040. str_table = get_unaligned_be32(&p[12]);
  2041. note_strings = get_unaligned_be32(&p[16]);
  2042. action_count = get_unaligned_be32(&p[48]);
  2043. proc_count = get_unaligned_be32(&p[52]);
  2044. if (index >= action_count)
  2045. return status;
  2046. act_name_id = get_unaligned_be32(&p[action_table + (12 * index)]);
  2047. act_desc_id = get_unaligned_be32(&p[action_table + (12 * index) + 4]);
  2048. act_proc_id = get_unaligned_be32(&p[action_table + (12 * index) + 8]);
  2049. *name = &p[str_table + act_name_id];
  2050. if (act_desc_id < (note_strings - str_table))
  2051. *description = &p[str_table + act_desc_id];
  2052. do {
  2053. act_proc_name = get_unaligned_be32(
  2054. &p[proc_table + (13 * act_proc_id)]);
  2055. act_proc_attribute =
  2056. (p[proc_table + (13 * act_proc_id) + 8] & 0x03);
  2057. procptr =
  2058. kzalloc(sizeof(struct altera_procinfo),
  2059. GFP_KERNEL);
  2060. if (procptr == NULL)
  2061. status = -ENOMEM;
  2062. else {
  2063. procptr->name = &p[str_table + act_proc_name];
  2064. procptr->attrs = act_proc_attribute;
  2065. procptr->next = NULL;
  2066. /* add record to end of linked list */
  2067. if (*proc_list == NULL)
  2068. *proc_list = procptr;
  2069. else {
  2070. tmpptr = *proc_list;
  2071. while (tmpptr->next != NULL)
  2072. tmpptr = tmpptr->next;
  2073. tmpptr->next = procptr;
  2074. }
  2075. }
  2076. act_proc_id = get_unaligned_be32(
  2077. &p[proc_table + (13 * act_proc_id) + 4]);
  2078. } while ((act_proc_id != 0) && (act_proc_id < proc_count));
  2079. return status;
  2080. }
  2081. int altera_init(struct altera_config *config, const struct firmware *fw)
  2082. {
  2083. struct altera_state *astate = NULL;
  2084. struct altera_procinfo *proc_list = NULL;
  2085. struct altera_procinfo *procptr = NULL;
  2086. char *key = NULL;
  2087. char *value = NULL;
  2088. char *action_name = NULL;
  2089. char *description = NULL;
  2090. int exec_result = 0;
  2091. int exit_code = 0;
  2092. int format_version = 0;
  2093. int action_count = 0;
  2094. int procedure_count = 0;
  2095. int index = 0;
  2096. s32 offset = 0L;
  2097. s32 error_address = 0L;
  2098. int retval = 0;
  2099. key = kzalloc(33, GFP_KERNEL);
  2100. if (!key) {
  2101. retval = -ENOMEM;
  2102. goto out;
  2103. }
  2104. value = kzalloc(257, GFP_KERNEL);
  2105. if (!value) {
  2106. retval = -ENOMEM;
  2107. goto free_key;
  2108. }
  2109. astate = kzalloc(sizeof(struct altera_state), GFP_KERNEL);
  2110. if (!astate) {
  2111. retval = -ENOMEM;
  2112. goto free_value;
  2113. }
  2114. astate->config = config;
  2115. if (!astate->config->jtag_io) {
  2116. if (!IS_ENABLED(CONFIG_HAS_IOPORT)) {
  2117. retval = -ENODEV;
  2118. goto free_state;
  2119. }
  2120. dprintk("%s: using byteblaster!\n", __func__);
  2121. astate->config->jtag_io = netup_jtag_io_lpt;
  2122. }
  2123. altera_check_crc((u8 *)fw->data, fw->size);
  2124. if (debug) {
  2125. altera_get_file_info((u8 *)fw->data, fw->size, &format_version,
  2126. &action_count, &procedure_count);
  2127. printk(KERN_INFO "%s: File format is %s ByteCode format\n",
  2128. __func__, (format_version == 2) ? "Jam STAPL" :
  2129. "pre-standardized Jam 1.1");
  2130. while (altera_get_note((u8 *)fw->data, fw->size,
  2131. &offset, key, value, 32, 256) == 0)
  2132. printk(KERN_INFO "%s: NOTE \"%s\" = \"%s\"\n",
  2133. __func__, key, value);
  2134. }
  2135. if (debug && (format_version == 2) && (action_count > 0)) {
  2136. printk(KERN_INFO "%s: Actions available:\n", __func__);
  2137. for (index = 0; index < action_count; ++index) {
  2138. altera_get_act_info((u8 *)fw->data, fw->size,
  2139. index, &action_name,
  2140. &description,
  2141. &proc_list);
  2142. if (description == NULL)
  2143. printk(KERN_INFO "%s: %s\n",
  2144. __func__,
  2145. action_name);
  2146. else
  2147. printk(KERN_INFO "%s: %s \"%s\"\n",
  2148. __func__,
  2149. action_name,
  2150. description);
  2151. procptr = proc_list;
  2152. while (procptr != NULL) {
  2153. if (procptr->attrs != 0)
  2154. printk(KERN_INFO "%s: %s (%s)\n",
  2155. __func__,
  2156. procptr->name,
  2157. (procptr->attrs == 1) ?
  2158. "optional" : "recommended");
  2159. proc_list = procptr->next;
  2160. kfree(procptr);
  2161. procptr = proc_list;
  2162. }
  2163. }
  2164. printk(KERN_INFO "\n");
  2165. }
  2166. exec_result = altera_execute(astate, (u8 *)fw->data, fw->size,
  2167. &error_address, &exit_code, &format_version);
  2168. if (exit_code)
  2169. exec_result = -EREMOTEIO;
  2170. if ((format_version == 2) && (exec_result == -EINVAL)) {
  2171. if (astate->config->action == NULL)
  2172. printk(KERN_ERR "%s: error: no action specified for "
  2173. "Jam STAPL file.\nprogram terminated.\n",
  2174. __func__);
  2175. else
  2176. printk(KERN_ERR "%s: error: action \"%s\""
  2177. " is not supported "
  2178. "for this Jam STAPL file.\n"
  2179. "Program terminated.\n", __func__,
  2180. astate->config->action);
  2181. } else if (exec_result)
  2182. printk(KERN_ERR "%s: error %d\n", __func__, exec_result);
  2183. free_state:
  2184. kfree(astate);
  2185. free_value:
  2186. kfree(value);
  2187. free_key:
  2188. kfree(key);
  2189. out:
  2190. return retval;
  2191. }
  2192. EXPORT_SYMBOL(altera_init);