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