0001
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012 #include <asm/unaligned.h>
0013 #include <linux/ctype.h>
0014 #include <linux/string.h>
0015 #include <linux/firmware.h>
0016 #include <linux/slab.h>
0017 #include <linux/module.h>
0018 #include <misc/altera.h>
0019 #include "altera-exprt.h"
0020 #include "altera-jtag.h"
0021
0022 static int debug = 1;
0023 module_param(debug, int, 0644);
0024 MODULE_PARM_DESC(debug, "enable debugging information");
0025
0026 MODULE_DESCRIPTION("altera FPGA kernel module");
0027 MODULE_AUTHOR("Igor M. Liplianin <liplianin@netup.ru>");
0028 MODULE_LICENSE("GPL");
0029
0030 #define dprintk(args...) \
0031 if (debug) { \
0032 printk(KERN_DEBUG args); \
0033 }
0034
0035 enum altera_fpga_opcode {
0036 OP_NOP = 0,
0037 OP_DUP,
0038 OP_SWP,
0039 OP_ADD,
0040 OP_SUB,
0041 OP_MULT,
0042 OP_DIV,
0043 OP_MOD,
0044 OP_SHL,
0045 OP_SHR,
0046 OP_NOT,
0047 OP_AND,
0048 OP_OR,
0049 OP_XOR,
0050 OP_INV,
0051 OP_GT,
0052 OP_LT,
0053 OP_RET,
0054 OP_CMPS,
0055 OP_PINT,
0056 OP_PRNT,
0057 OP_DSS,
0058 OP_DSSC,
0059 OP_ISS,
0060 OP_ISSC,
0061 OP_DPR = 0x1c,
0062 OP_DPRL,
0063 OP_DPO,
0064 OP_DPOL,
0065 OP_IPR,
0066 OP_IPRL,
0067 OP_IPO,
0068 OP_IPOL,
0069 OP_PCHR,
0070 OP_EXIT,
0071 OP_EQU,
0072 OP_POPT,
0073 OP_ABS = 0x2c,
0074 OP_BCH0,
0075 OP_PSH0 = 0x2f,
0076 OP_PSHL = 0x40,
0077 OP_PSHV,
0078 OP_JMP,
0079 OP_CALL,
0080 OP_NEXT,
0081 OP_PSTR,
0082 OP_SINT = 0x47,
0083 OP_ST,
0084 OP_ISTP,
0085 OP_DSTP,
0086 OP_SWPN,
0087 OP_DUPN,
0088 OP_POPV,
0089 OP_POPE,
0090 OP_POPA,
0091 OP_JMPZ,
0092 OP_DS,
0093 OP_IS,
0094 OP_DPRA,
0095 OP_DPOA,
0096 OP_IPRA,
0097 OP_IPOA,
0098 OP_EXPT,
0099 OP_PSHE,
0100 OP_PSHA,
0101 OP_DYNA,
0102 OP_EXPV = 0x5c,
0103 OP_COPY = 0x80,
0104 OP_REVA,
0105 OP_DSC,
0106 OP_ISC,
0107 OP_WAIT,
0108 OP_VS,
0109 OP_CMPA = 0xc0,
0110 OP_VSC,
0111 };
0112
0113 struct altera_procinfo {
0114 char *name;
0115 u8 attrs;
0116 struct altera_procinfo *next;
0117 };
0118
0119
0120 static int altera_check_stack(int stack_ptr, int count, int *status)
0121 {
0122 if (stack_ptr < count) {
0123 *status = -EOVERFLOW;
0124 return 0;
0125 }
0126
0127 return 1;
0128 }
0129
0130 static void altera_export_int(char *key, s32 value)
0131 {
0132 dprintk("Export: key = \"%s\", value = %d\n", key, value);
0133 }
0134
0135 #define HEX_LINE_CHARS 72
0136 #define HEX_LINE_BITS (HEX_LINE_CHARS * 4)
0137
0138 static void altera_export_bool_array(char *key, u8 *data, s32 count)
0139 {
0140 char string[HEX_LINE_CHARS + 1];
0141 s32 i, offset;
0142 u32 size, line, lines, linebits, value, j, k;
0143
0144 if (count > HEX_LINE_BITS) {
0145 dprintk("Export: key = \"%s\", %d bits, value = HEX\n",
0146 key, count);
0147 lines = (count + (HEX_LINE_BITS - 1)) / HEX_LINE_BITS;
0148
0149 for (line = 0; line < lines; ++line) {
0150 if (line < (lines - 1)) {
0151 linebits = HEX_LINE_BITS;
0152 size = HEX_LINE_CHARS;
0153 offset = count - ((line + 1) * HEX_LINE_BITS);
0154 } else {
0155 linebits =
0156 count - ((lines - 1) * HEX_LINE_BITS);
0157 size = (linebits + 3) / 4;
0158 offset = 0L;
0159 }
0160
0161 string[size] = '\0';
0162 j = size - 1;
0163 value = 0;
0164
0165 for (k = 0; k < linebits; ++k) {
0166 i = k + offset;
0167 if (data[i >> 3] & (1 << (i & 7)))
0168 value |= (1 << (i & 3));
0169 if ((i & 3) == 3) {
0170 sprintf(&string[j], "%1x", value);
0171 value = 0;
0172 --j;
0173 }
0174 }
0175 if ((k & 3) > 0)
0176 sprintf(&string[j], "%1x", value);
0177
0178 dprintk("%s\n", string);
0179 }
0180
0181 } else {
0182 size = (count + 3) / 4;
0183 string[size] = '\0';
0184 j = size - 1;
0185 value = 0;
0186
0187 for (i = 0; i < count; ++i) {
0188 if (data[i >> 3] & (1 << (i & 7)))
0189 value |= (1 << (i & 3));
0190 if ((i & 3) == 3) {
0191 sprintf(&string[j], "%1x", value);
0192 value = 0;
0193 --j;
0194 }
0195 }
0196 if ((i & 3) > 0)
0197 sprintf(&string[j], "%1x", value);
0198
0199 dprintk("Export: key = \"%s\", %d bits, value = HEX %s\n",
0200 key, count, string);
0201 }
0202 }
0203
0204 static int altera_execute(struct altera_state *astate,
0205 u8 *p,
0206 s32 program_size,
0207 s32 *error_address,
0208 int *exit_code,
0209 int *format_version)
0210 {
0211 struct altera_config *aconf = astate->config;
0212 char *msg_buff = astate->msg_buff;
0213 long *stack = astate->stack;
0214 int status = 0;
0215 u32 first_word = 0L;
0216 u32 action_table = 0L;
0217 u32 proc_table = 0L;
0218 u32 str_table = 0L;
0219 u32 sym_table = 0L;
0220 u32 data_sect = 0L;
0221 u32 code_sect = 0L;
0222 u32 debug_sect = 0L;
0223 u32 action_count = 0L;
0224 u32 proc_count = 0L;
0225 u32 sym_count = 0L;
0226 long *vars = NULL;
0227 s32 *var_size = NULL;
0228 char *attrs = NULL;
0229 u8 *proc_attributes = NULL;
0230 u32 pc;
0231 u32 opcode_address;
0232 u32 args[3];
0233 u32 opcode;
0234 u32 name_id;
0235 u8 charbuf[4];
0236 long long_tmp;
0237 u32 variable_id;
0238 u8 *charptr_tmp;
0239 u8 *charptr_tmp2;
0240 long *longptr_tmp;
0241 int version = 0;
0242 int delta = 0;
0243 int stack_ptr = 0;
0244 u32 arg_count;
0245 int done = 0;
0246 int bad_opcode = 0;
0247 u32 count;
0248 u32 index;
0249 u32 index2;
0250 s32 long_count;
0251 s32 long_idx;
0252 s32 long_idx2;
0253 u32 i;
0254 u32 j;
0255 u32 uncomp_size;
0256 u32 offset;
0257 u32 value;
0258 int current_proc = 0;
0259 int reverse;
0260
0261 char *name;
0262
0263 dprintk("%s\n", __func__);
0264
0265
0266 if (program_size > 52L) {
0267 first_word = get_unaligned_be32(&p[0]);
0268 version = (first_word & 1L);
0269 *format_version = version + 1;
0270 delta = version * 8;
0271
0272 action_table = get_unaligned_be32(&p[4]);
0273 proc_table = get_unaligned_be32(&p[8]);
0274 str_table = get_unaligned_be32(&p[4 + delta]);
0275 sym_table = get_unaligned_be32(&p[16 + delta]);
0276 data_sect = get_unaligned_be32(&p[20 + delta]);
0277 code_sect = get_unaligned_be32(&p[24 + delta]);
0278 debug_sect = get_unaligned_be32(&p[28 + delta]);
0279 action_count = get_unaligned_be32(&p[40 + delta]);
0280 proc_count = get_unaligned_be32(&p[44 + delta]);
0281 sym_count = get_unaligned_be32(&p[48 + (2 * delta)]);
0282 }
0283
0284 if ((first_word != 0x4A414D00L) && (first_word != 0x4A414D01L)) {
0285 done = 1;
0286 status = -EIO;
0287 goto exit_done;
0288 }
0289
0290 if (sym_count <= 0)
0291 goto exit_done;
0292
0293 vars = kcalloc(sym_count, sizeof(long), GFP_KERNEL);
0294
0295 if (vars == NULL)
0296 status = -ENOMEM;
0297
0298 if (status == 0) {
0299 var_size = kcalloc(sym_count, sizeof(s32), GFP_KERNEL);
0300
0301 if (var_size == NULL)
0302 status = -ENOMEM;
0303 }
0304
0305 if (status == 0) {
0306 attrs = kzalloc(sym_count, GFP_KERNEL);
0307
0308 if (attrs == NULL)
0309 status = -ENOMEM;
0310 }
0311
0312 if ((status == 0) && (version > 0)) {
0313 proc_attributes = kzalloc(proc_count, GFP_KERNEL);
0314
0315 if (proc_attributes == NULL)
0316 status = -ENOMEM;
0317 }
0318
0319 if (status != 0)
0320 goto exit_done;
0321
0322 delta = version * 2;
0323
0324 for (i = 0; i < sym_count; ++i) {
0325 offset = (sym_table + ((11 + delta) * i));
0326
0327 value = get_unaligned_be32(&p[offset + 3 + delta]);
0328
0329 attrs[i] = p[offset];
0330
0331
0332
0333
0334
0335
0336 attrs[i] &= 0x7f;
0337
0338 var_size[i] = get_unaligned_be32(&p[offset + 7 + delta]);
0339
0340
0341
0342
0343
0344
0345
0346
0347
0348
0349
0350
0351 if ((attrs[i] & 0x0c) == 0x04)
0352
0353 vars[i] = value;
0354 else if ((attrs[i] & 0x1e) == 0x0e) {
0355
0356 uncomp_size = get_unaligned_le32(&p[data_sect + value]);
0357
0358
0359 vars[i] = (long)kzalloc(uncomp_size, GFP_KERNEL);
0360 if (vars[i] == 0L)
0361 status = -ENOMEM;
0362 else {
0363
0364 attrs[i] |= 0x80;
0365
0366
0367 if (altera_shrink(&p[data_sect + value],
0368 var_size[i],
0369 (u8 *)vars[i],
0370 uncomp_size,
0371 version) != uncomp_size)
0372
0373 status = -EIO;
0374 else
0375 var_size[i] = uncomp_size * 8L;
0376
0377 }
0378 } else if ((attrs[i] & 0x1e) == 0x0c) {
0379
0380 vars[i] = value + data_sect + (long)p;
0381 } else if ((attrs[i] & 0x1c) == 0x1c) {
0382
0383 vars[i] = value + data_sect;
0384 } else if ((attrs[i] & 0x0c) == 0x08) {
0385
0386
0387
0388 attrs[i] |= 0x80;
0389
0390 if (var_size[i] > 0) {
0391 u32 size;
0392
0393 if (attrs[i] & 0x10)
0394
0395 size = (var_size[i] * sizeof(s32));
0396 else
0397
0398 size = ((var_size[i] + 7L) / 8L);
0399
0400 vars[i] = (long)kzalloc(size, GFP_KERNEL);
0401
0402 if (vars[i] == 0) {
0403 status = -ENOMEM;
0404 } else {
0405
0406 for (j = 0; j < size; ++j)
0407 ((u8 *)(vars[i]))[j] = 0;
0408
0409 }
0410 } else
0411 vars[i] = 0;
0412
0413 } else
0414 vars[i] = 0;
0415
0416 }
0417
0418 exit_done:
0419 if (status != 0)
0420 done = 1;
0421
0422 altera_jinit(astate);
0423
0424 pc = code_sect;
0425 msg_buff[0] = '\0';
0426
0427
0428
0429
0430
0431 if (version > 0) {
0432 if (aconf->action == NULL) {
0433 status = -EINVAL;
0434 done = 1;
0435 } else {
0436 int action_found = 0;
0437 for (i = 0; (i < action_count) && !action_found; ++i) {
0438 name_id = get_unaligned_be32(&p[action_table +
0439 (12 * i)]);
0440
0441 name = &p[str_table + name_id];
0442
0443 if (strncasecmp(aconf->action, name, strlen(name)) == 0) {
0444 action_found = 1;
0445 current_proc =
0446 get_unaligned_be32(&p[action_table +
0447 (12 * i) + 8]);
0448 }
0449 }
0450
0451 if (!action_found) {
0452 status = -EINVAL;
0453 done = 1;
0454 }
0455 }
0456
0457 if (status == 0) {
0458 int first_time = 1;
0459 i = current_proc;
0460 while ((i != 0) || first_time) {
0461 first_time = 0;
0462
0463 proc_attributes[i] =
0464 (p[proc_table +
0465 (13 * i) + 8] &
0466 0x03);
0467
0468
0469
0470
0471
0472
0473
0474
0475 i = get_unaligned_be32(&p[proc_table +
0476 (13 * i) + 4]);
0477 }
0478
0479
0480
0481
0482
0483 i = current_proc;
0484 while ((i != 0) &&
0485 ((proc_attributes[i] == 1) ||
0486 ((proc_attributes[i] & 0xc0) == 0x40))) {
0487 i = get_unaligned_be32(&p[proc_table +
0488 (13 * i) + 4]);
0489 }
0490
0491 if ((i != 0) || ((i == 0) && (current_proc == 0) &&
0492 ((proc_attributes[0] != 1) &&
0493 ((proc_attributes[0] & 0xc0) != 0x40)))) {
0494 current_proc = i;
0495 pc = code_sect +
0496 get_unaligned_be32(&p[proc_table +
0497 (13 * i) + 9]);
0498 if ((pc < code_sect) || (pc >= debug_sect))
0499 status = -ERANGE;
0500 } else
0501
0502 done = 1;
0503
0504 }
0505 }
0506
0507 msg_buff[0] = '\0';
0508
0509 while (!done) {
0510 opcode = (p[pc] & 0xff);
0511 opcode_address = pc;
0512 ++pc;
0513
0514 if (debug > 1)
0515 printk("opcode: %02x\n", opcode);
0516
0517 arg_count = (opcode >> 6) & 3;
0518 for (i = 0; i < arg_count; ++i) {
0519 args[i] = get_unaligned_be32(&p[pc]);
0520 pc += 4;
0521 }
0522
0523 switch (opcode) {
0524 case OP_NOP:
0525 break;
0526 case OP_DUP:
0527 if (altera_check_stack(stack_ptr, 1, &status)) {
0528 stack[stack_ptr] = stack[stack_ptr - 1];
0529 ++stack_ptr;
0530 }
0531 break;
0532 case OP_SWP:
0533 if (altera_check_stack(stack_ptr, 2, &status))
0534 swap(stack[stack_ptr - 2], stack[stack_ptr - 1]);
0535 break;
0536 case OP_ADD:
0537 if (altera_check_stack(stack_ptr, 2, &status)) {
0538 --stack_ptr;
0539 stack[stack_ptr - 1] += stack[stack_ptr];
0540 }
0541 break;
0542 case OP_SUB:
0543 if (altera_check_stack(stack_ptr, 2, &status)) {
0544 --stack_ptr;
0545 stack[stack_ptr - 1] -= stack[stack_ptr];
0546 }
0547 break;
0548 case OP_MULT:
0549 if (altera_check_stack(stack_ptr, 2, &status)) {
0550 --stack_ptr;
0551 stack[stack_ptr - 1] *= stack[stack_ptr];
0552 }
0553 break;
0554 case OP_DIV:
0555 if (altera_check_stack(stack_ptr, 2, &status)) {
0556 --stack_ptr;
0557 stack[stack_ptr - 1] /= stack[stack_ptr];
0558 }
0559 break;
0560 case OP_MOD:
0561 if (altera_check_stack(stack_ptr, 2, &status)) {
0562 --stack_ptr;
0563 stack[stack_ptr - 1] %= stack[stack_ptr];
0564 }
0565 break;
0566 case OP_SHL:
0567 if (altera_check_stack(stack_ptr, 2, &status)) {
0568 --stack_ptr;
0569 stack[stack_ptr - 1] <<= stack[stack_ptr];
0570 }
0571 break;
0572 case OP_SHR:
0573 if (altera_check_stack(stack_ptr, 2, &status)) {
0574 --stack_ptr;
0575 stack[stack_ptr - 1] >>= stack[stack_ptr];
0576 }
0577 break;
0578 case OP_NOT:
0579 if (altera_check_stack(stack_ptr, 1, &status))
0580 stack[stack_ptr - 1] ^= (-1L);
0581
0582 break;
0583 case OP_AND:
0584 if (altera_check_stack(stack_ptr, 2, &status)) {
0585 --stack_ptr;
0586 stack[stack_ptr - 1] &= stack[stack_ptr];
0587 }
0588 break;
0589 case OP_OR:
0590 if (altera_check_stack(stack_ptr, 2, &status)) {
0591 --stack_ptr;
0592 stack[stack_ptr - 1] |= stack[stack_ptr];
0593 }
0594 break;
0595 case OP_XOR:
0596 if (altera_check_stack(stack_ptr, 2, &status)) {
0597 --stack_ptr;
0598 stack[stack_ptr - 1] ^= stack[stack_ptr];
0599 }
0600 break;
0601 case OP_INV:
0602 if (!altera_check_stack(stack_ptr, 1, &status))
0603 break;
0604 stack[stack_ptr - 1] = stack[stack_ptr - 1] ? 0L : 1L;
0605 break;
0606 case OP_GT:
0607 if (!altera_check_stack(stack_ptr, 2, &status))
0608 break;
0609 --stack_ptr;
0610 stack[stack_ptr - 1] =
0611 (stack[stack_ptr - 1] > stack[stack_ptr]) ?
0612 1L : 0L;
0613
0614 break;
0615 case OP_LT:
0616 if (!altera_check_stack(stack_ptr, 2, &status))
0617 break;
0618 --stack_ptr;
0619 stack[stack_ptr - 1] =
0620 (stack[stack_ptr - 1] < stack[stack_ptr]) ?
0621 1L : 0L;
0622
0623 break;
0624 case OP_RET:
0625 if ((version > 0) && (stack_ptr == 0)) {
0626
0627
0628
0629
0630
0631
0632
0633 i = get_unaligned_be32(&p[proc_table +
0634 (13 * current_proc) + 4]);
0635 while ((i != 0) &&
0636 ((proc_attributes[i] == 1) ||
0637 ((proc_attributes[i] & 0xc0) == 0x40)))
0638 i = get_unaligned_be32(&p[proc_table +
0639 (13 * i) + 4]);
0640
0641 if (i == 0) {
0642
0643 done = 1;
0644 *exit_code = 0;
0645 } else {
0646 current_proc = i;
0647 pc = code_sect + get_unaligned_be32(
0648 &p[proc_table +
0649 (13 * i) + 9]);
0650 if ((pc < code_sect) ||
0651 (pc >= debug_sect))
0652 status = -ERANGE;
0653 }
0654
0655 } else
0656 if (altera_check_stack(stack_ptr, 1, &status)) {
0657 pc = stack[--stack_ptr] + code_sect;
0658 if ((pc <= code_sect) ||
0659 (pc >= debug_sect))
0660 status = -ERANGE;
0661
0662 }
0663
0664 break;
0665 case OP_CMPS:
0666
0667
0668
0669
0670
0671
0672
0673 if (altera_check_stack(stack_ptr, 4, &status)) {
0674 s32 a = stack[--stack_ptr];
0675 s32 b = stack[--stack_ptr];
0676 long_tmp = stack[--stack_ptr];
0677 count = stack[stack_ptr - 1];
0678
0679 if ((count < 1) || (count > 32))
0680 status = -ERANGE;
0681 else {
0682 long_tmp &= ((-1L) >> (32 - count));
0683
0684 stack[stack_ptr - 1] =
0685 ((a & long_tmp) == (b & long_tmp))
0686 ? 1L : 0L;
0687 }
0688 }
0689 break;
0690 case OP_PINT:
0691
0692
0693
0694
0695 if (!altera_check_stack(stack_ptr, 1, &status))
0696 break;
0697 sprintf(&msg_buff[strlen(msg_buff)],
0698 "%ld", stack[--stack_ptr]);
0699 break;
0700 case OP_PRNT:
0701
0702 if (debug)
0703 printk(msg_buff, "\n");
0704
0705 msg_buff[0] = '\0';
0706 break;
0707 case OP_DSS:
0708
0709
0710
0711
0712
0713 if (!altera_check_stack(stack_ptr, 2, &status))
0714 break;
0715 long_tmp = stack[--stack_ptr];
0716 count = stack[--stack_ptr];
0717 put_unaligned_le32(long_tmp, &charbuf[0]);
0718 status = altera_drscan(astate, count, charbuf, 0);
0719 break;
0720 case OP_DSSC:
0721
0722
0723
0724
0725
0726 if (!altera_check_stack(stack_ptr, 2, &status))
0727 break;
0728 long_tmp = stack[--stack_ptr];
0729 count = stack[stack_ptr - 1];
0730 put_unaligned_le32(long_tmp, &charbuf[0]);
0731 status = altera_swap_dr(astate, count, charbuf,
0732 0, charbuf, 0);
0733 stack[stack_ptr - 1] = get_unaligned_le32(&charbuf[0]);
0734 break;
0735 case OP_ISS:
0736
0737
0738
0739
0740
0741 if (!altera_check_stack(stack_ptr, 2, &status))
0742 break;
0743 long_tmp = stack[--stack_ptr];
0744 count = stack[--stack_ptr];
0745 put_unaligned_le32(long_tmp, &charbuf[0]);
0746 status = altera_irscan(astate, count, charbuf, 0);
0747 break;
0748 case OP_ISSC:
0749
0750
0751
0752
0753
0754 if (!altera_check_stack(stack_ptr, 2, &status))
0755 break;
0756 long_tmp = stack[--stack_ptr];
0757 count = stack[stack_ptr - 1];
0758 put_unaligned_le32(long_tmp, &charbuf[0]);
0759 status = altera_swap_ir(astate, count, charbuf,
0760 0, charbuf, 0);
0761 stack[stack_ptr - 1] = get_unaligned_le32(&charbuf[0]);
0762 break;
0763 case OP_DPR:
0764 if (!altera_check_stack(stack_ptr, 1, &status))
0765 break;
0766 count = stack[--stack_ptr];
0767 status = altera_set_dr_pre(&astate->js, count, 0, NULL);
0768 break;
0769 case OP_DPRL:
0770
0771
0772
0773
0774
0775 if (!altera_check_stack(stack_ptr, 2, &status))
0776 break;
0777 count = stack[--stack_ptr];
0778 long_tmp = stack[--stack_ptr];
0779 put_unaligned_le32(long_tmp, &charbuf[0]);
0780 status = altera_set_dr_pre(&astate->js, count, 0,
0781 charbuf);
0782 break;
0783 case OP_DPO:
0784
0785
0786
0787
0788 if (altera_check_stack(stack_ptr, 1, &status)) {
0789 count = stack[--stack_ptr];
0790 status = altera_set_dr_post(&astate->js, count,
0791 0, NULL);
0792 }
0793 break;
0794 case OP_DPOL:
0795
0796
0797
0798
0799
0800 if (!altera_check_stack(stack_ptr, 2, &status))
0801 break;
0802 count = stack[--stack_ptr];
0803 long_tmp = stack[--stack_ptr];
0804 put_unaligned_le32(long_tmp, &charbuf[0]);
0805 status = altera_set_dr_post(&astate->js, count, 0,
0806 charbuf);
0807 break;
0808 case OP_IPR:
0809 if (altera_check_stack(stack_ptr, 1, &status)) {
0810 count = stack[--stack_ptr];
0811 status = altera_set_ir_pre(&astate->js, count,
0812 0, NULL);
0813 }
0814 break;
0815 case OP_IPRL:
0816
0817
0818
0819
0820
0821 if (altera_check_stack(stack_ptr, 2, &status)) {
0822 count = stack[--stack_ptr];
0823 long_tmp = stack[--stack_ptr];
0824 put_unaligned_le32(long_tmp, &charbuf[0]);
0825 status = altera_set_ir_pre(&astate->js, count,
0826 0, charbuf);
0827 }
0828 break;
0829 case OP_IPO:
0830
0831
0832
0833
0834 if (altera_check_stack(stack_ptr, 1, &status)) {
0835 count = stack[--stack_ptr];
0836 status = altera_set_ir_post(&astate->js, count,
0837 0, NULL);
0838 }
0839 break;
0840 case OP_IPOL:
0841
0842
0843
0844
0845
0846 if (!altera_check_stack(stack_ptr, 2, &status))
0847 break;
0848 count = stack[--stack_ptr];
0849 long_tmp = stack[--stack_ptr];
0850 put_unaligned_le32(long_tmp, &charbuf[0]);
0851 status = altera_set_ir_post(&astate->js, count, 0,
0852 charbuf);
0853 break;
0854 case OP_PCHR:
0855 if (altera_check_stack(stack_ptr, 1, &status)) {
0856 u8 ch;
0857 count = strlen(msg_buff);
0858 ch = (char) stack[--stack_ptr];
0859 if ((ch < 1) || (ch > 127)) {
0860
0861
0862
0863
0864
0865 ch = 127;
0866 }
0867 msg_buff[count] = ch;
0868 msg_buff[count + 1] = '\0';
0869 }
0870 break;
0871 case OP_EXIT:
0872 if (altera_check_stack(stack_ptr, 1, &status))
0873 *exit_code = stack[--stack_ptr];
0874
0875 done = 1;
0876 break;
0877 case OP_EQU:
0878 if (!altera_check_stack(stack_ptr, 2, &status))
0879 break;
0880 --stack_ptr;
0881 stack[stack_ptr - 1] =
0882 (stack[stack_ptr - 1] == stack[stack_ptr]) ?
0883 1L : 0L;
0884 break;
0885 case OP_POPT:
0886 if (altera_check_stack(stack_ptr, 1, &status))
0887 --stack_ptr;
0888
0889 break;
0890 case OP_ABS:
0891 if (!altera_check_stack(stack_ptr, 1, &status))
0892 break;
0893 if (stack[stack_ptr - 1] < 0)
0894 stack[stack_ptr - 1] = 0 - stack[stack_ptr - 1];
0895
0896 break;
0897 case OP_BCH0:
0898
0899
0900
0901
0902
0903
0904
0905
0906
0907
0908
0909
0910
0911
0912 if (altera_check_stack(stack_ptr, 2, &status))
0913 swap(stack[stack_ptr - 2], stack[stack_ptr - 1]);
0914
0915
0916 index = 7 + 1;
0917 if (altera_check_stack(stack_ptr, index, &status))
0918 swap(stack[stack_ptr - index], stack[stack_ptr - 1]);
0919
0920
0921 if (altera_check_stack(stack_ptr, 2, &status))
0922 swap(stack[stack_ptr - 2], stack[stack_ptr - 1]);
0923
0924
0925 index = 6 + 1;
0926 if (altera_check_stack(stack_ptr, index, &status))
0927 swap(stack[stack_ptr - index], stack[stack_ptr - 1]);
0928
0929
0930 index = 8 + 1;
0931 if (altera_check_stack(stack_ptr, index, &status)) {
0932 stack[stack_ptr] = stack[stack_ptr - index];
0933 ++stack_ptr;
0934 }
0935
0936
0937 index = 2 + 1;
0938 if (altera_check_stack(stack_ptr, index, &status))
0939 swap(stack[stack_ptr - index], stack[stack_ptr - 1]);
0940
0941
0942 if (altera_check_stack(stack_ptr, 2, &status))
0943 swap(stack[stack_ptr - 2], stack[stack_ptr - 1]);
0944
0945
0946 index = 6 + 1;
0947 if (altera_check_stack(stack_ptr, index, &status)) {
0948 stack[stack_ptr] = stack[stack_ptr - index];
0949 ++stack_ptr;
0950 }
0951
0952
0953 index = 6 + 1;
0954 if (altera_check_stack(stack_ptr, index, &status)) {
0955 stack[stack_ptr] = stack[stack_ptr - index];
0956 ++stack_ptr;
0957 }
0958 break;
0959 case OP_PSH0:
0960 stack[stack_ptr++] = 0;
0961 break;
0962 case OP_PSHL:
0963 stack[stack_ptr++] = (s32) args[0];
0964 break;
0965 case OP_PSHV:
0966 stack[stack_ptr++] = vars[args[0]];
0967 break;
0968 case OP_JMP:
0969 pc = args[0] + code_sect;
0970 if ((pc < code_sect) || (pc >= debug_sect))
0971 status = -ERANGE;
0972 break;
0973 case OP_CALL:
0974 stack[stack_ptr++] = pc;
0975 pc = args[0] + code_sect;
0976 if ((pc < code_sect) || (pc >= debug_sect))
0977 status = -ERANGE;
0978 break;
0979 case OP_NEXT:
0980
0981
0982
0983
0984
0985
0986
0987 if (altera_check_stack(stack_ptr, 3, &status)) {
0988 s32 step = stack[stack_ptr - 1];
0989 s32 end = stack[stack_ptr - 2];
0990 s32 top = stack[stack_ptr - 3];
0991 s32 iterator = vars[args[0]];
0992 int break_out = 0;
0993
0994 if (step < 0) {
0995 if (iterator <= end)
0996 break_out = 1;
0997 } else if (iterator >= end)
0998 break_out = 1;
0999
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
1014
1015
1016 count = strlen(msg_buff);
1017 strlcpy(&msg_buff[count],
1018 &p[str_table + args[0]],
1019 ALTERA_MESSAGE_LENGTH - count);
1020 break;
1021 case OP_SINT:
1022
1023
1024
1025
1026 status = altera_goto_jstate(astate, args[0]);
1027 break;
1028 case OP_ST:
1029
1030
1031
1032
1033 status = altera_goto_jstate(astate, args[0]);
1034 break;
1035 case OP_ISTP:
1036
1037
1038
1039
1040 status = altera_set_irstop(&astate->js, args[0]);
1041 break;
1042 case OP_DSTP:
1043
1044
1045
1046
1047 status = altera_set_drstop(&astate->js, args[0]);
1048 break;
1049
1050 case OP_SWPN:
1051
1052
1053
1054
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
1063
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
1074
1075
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
1084
1085
1086
1087
1088 if (!altera_check_stack(stack_ptr, 2, &status))
1089 break;
1090 variable_id = args[0];
1091
1092
1093
1094
1095
1096 if ((version > 0) &&
1097 ((attrs[variable_id] & 0x9c) == 0x1c)) {
1098
1099 count = var_size[variable_id];
1100 long_tmp = vars[variable_id];
1101 longptr_tmp = kcalloc(count, sizeof(long),
1102 GFP_KERNEL);
1103 vars[variable_id] = (long)longptr_tmp;
1104
1105 if (vars[variable_id] == 0) {
1106 status = -ENOMEM;
1107 break;
1108 }
1109
1110
1111 for (i = 0; i < count; ++i) {
1112 longptr_tmp[i] =
1113 get_unaligned_be32(&p[long_tmp]);
1114 long_tmp += sizeof(long);
1115 }
1116
1117
1118
1119
1120
1121 attrs[variable_id] |= 0x80;
1122
1123
1124 attrs[variable_id] &= ~0x04;
1125 attrs[variable_id] |= 0x01;
1126
1127 }
1128
1129
1130 if ((attrs[variable_id] & 0x1c) != 0x18)
1131 status = -ERANGE;
1132 else {
1133 longptr_tmp = (long *)vars[variable_id];
1134
1135
1136 index = stack[--stack_ptr];
1137
1138
1139 longptr_tmp[index] = stack[--stack_ptr];
1140 }
1141
1142 break;
1143 case OP_POPA:
1144
1145
1146
1147
1148
1149
1150
1151 if (!altera_check_stack(stack_ptr, 3, &status))
1152 break;
1153 variable_id = args[0];
1154
1155
1156
1157
1158
1159 if ((version > 0) &&
1160 ((attrs[variable_id] & 0x9c) == 0x0c)) {
1161
1162 long_tmp =
1163 (var_size[variable_id] + 7L) >> 3L;
1164 charptr_tmp2 = (u8 *)vars[variable_id];
1165 charptr_tmp =
1166 kzalloc(long_tmp, GFP_KERNEL);
1167 vars[variable_id] = (long)charptr_tmp;
1168
1169 if (vars[variable_id] == 0) {
1170 status = -ENOMEM;
1171 break;
1172 }
1173
1174
1175 for (long_idx = 0L;
1176 long_idx < long_tmp;
1177 ++long_idx) {
1178 charptr_tmp[long_idx] = 0;
1179 }
1180
1181
1182 for (long_idx = 0L;
1183 long_idx < var_size[variable_id];
1184 ++long_idx) {
1185 long_idx2 = long_idx;
1186
1187 if (charptr_tmp2[long_idx2 >> 3] &
1188 (1 << (long_idx2 & 7))) {
1189 charptr_tmp[long_idx >> 3] |=
1190 (1 << (long_idx & 7));
1191 }
1192 }
1193
1194
1195
1196
1197
1198 attrs[variable_id] |= 0x80;
1199
1200
1201 attrs[variable_id] &= ~0x04;
1202 attrs[variable_id] |= 0x01;
1203
1204 }
1205
1206
1207
1208
1209
1210 if ((attrs[variable_id] & 0x1c) != 0x08) {
1211 status = -ERANGE;
1212 break;
1213 }
1214
1215 charptr_tmp = (u8 *)vars[variable_id];
1216
1217
1218 long_count = stack[--stack_ptr];
1219
1220
1221 long_idx = stack[--stack_ptr];
1222
1223 reverse = 0;
1224
1225 if (version > 0) {
1226
1227
1228
1229
1230
1231 if (long_idx > long_count) {
1232 reverse = 1;
1233 long_tmp = long_count;
1234 long_count = 1 + long_idx -
1235 long_count;
1236 long_idx = long_tmp;
1237
1238
1239 status = -ERANGE;
1240 break;
1241 } else
1242 long_count = 1 + long_count -
1243 long_idx;
1244
1245 }
1246
1247
1248 long_tmp = stack[--stack_ptr];
1249
1250 if (long_count < 1) {
1251 status = -ERANGE;
1252 break;
1253 }
1254
1255 for (i = 0; i < long_count; ++i) {
1256 if (long_tmp & (1L << (s32) i))
1257 charptr_tmp[long_idx >> 3L] |=
1258 (1L << (long_idx & 7L));
1259 else
1260 charptr_tmp[long_idx >> 3L] &=
1261 ~(1L << (long_idx & 7L));
1262
1263 ++long_idx;
1264 }
1265
1266 break;
1267 case OP_JMPZ:
1268
1269
1270
1271
1272
1273 if (altera_check_stack(stack_ptr, 1, &status)) {
1274 if (stack[--stack_ptr] == 0) {
1275 pc = args[0] + code_sect;
1276 if ((pc < code_sect) ||
1277 (pc >= debug_sect))
1278 status = -ERANGE;
1279 }
1280 }
1281 break;
1282 case OP_DS:
1283 case OP_IS:
1284
1285
1286
1287
1288
1289
1290
1291 if (!altera_check_stack(stack_ptr, 2, &status))
1292 break;
1293 long_idx = stack[--stack_ptr];
1294 long_count = stack[--stack_ptr];
1295 reverse = 0;
1296 if (version > 0) {
1297
1298
1299
1300
1301
1302 long_tmp = long_count;
1303 long_count = stack[--stack_ptr];
1304
1305 if (long_idx > long_tmp) {
1306 reverse = 1;
1307 long_idx = long_tmp;
1308 }
1309 }
1310
1311 charptr_tmp = (u8 *)vars[args[0]];
1312
1313 if (reverse) {
1314
1315
1316
1317
1318 charptr_tmp2 = charptr_tmp;
1319 charptr_tmp = kzalloc((long_count >> 3) + 1,
1320 GFP_KERNEL);
1321 if (charptr_tmp == NULL) {
1322 status = -ENOMEM;
1323 break;
1324 }
1325
1326 long_tmp = long_idx + long_count - 1;
1327 long_idx2 = 0;
1328 while (long_idx2 < long_count) {
1329 if (charptr_tmp2[long_tmp >> 3] &
1330 (1 << (long_tmp & 7)))
1331 charptr_tmp[long_idx2 >> 3] |=
1332 (1 << (long_idx2 & 7));
1333 else
1334 charptr_tmp[long_idx2 >> 3] &=
1335 ~(1 << (long_idx2 & 7));
1336
1337 --long_tmp;
1338 ++long_idx2;
1339 }
1340 }
1341
1342 if (opcode == 0x51)
1343 status = altera_drscan(astate, long_count,
1344 charptr_tmp, long_idx);
1345 else
1346 status = altera_irscan(astate, long_count,
1347 charptr_tmp, long_idx);
1348
1349 if (reverse)
1350 kfree(charptr_tmp);
1351
1352 break;
1353 case OP_DPRA:
1354
1355
1356
1357
1358
1359
1360 if (!altera_check_stack(stack_ptr, 2, &status))
1361 break;
1362 index = stack[--stack_ptr];
1363 count = stack[--stack_ptr];
1364
1365 if (version > 0)
1366
1367
1368
1369
1370 count = 1 + count - index;
1371
1372 charptr_tmp = (u8 *)vars[args[0]];
1373 status = altera_set_dr_pre(&astate->js, count, index,
1374 charptr_tmp);
1375 break;
1376 case OP_DPOA:
1377
1378
1379
1380
1381
1382
1383 if (!altera_check_stack(stack_ptr, 2, &status))
1384 break;
1385 index = stack[--stack_ptr];
1386 count = stack[--stack_ptr];
1387
1388 if (version > 0)
1389
1390
1391
1392
1393 count = 1 + count - index;
1394
1395 charptr_tmp = (u8 *)vars[args[0]];
1396 status = altera_set_dr_post(&astate->js, count, index,
1397 charptr_tmp);
1398 break;
1399 case OP_IPRA:
1400
1401
1402
1403
1404
1405
1406 if (!altera_check_stack(stack_ptr, 2, &status))
1407 break;
1408 index = stack[--stack_ptr];
1409 count = stack[--stack_ptr];
1410
1411 if (version > 0)
1412
1413
1414
1415
1416 count = 1 + count - index;
1417
1418 charptr_tmp = (u8 *)vars[args[0]];
1419 status = altera_set_ir_pre(&astate->js, count, index,
1420 charptr_tmp);
1421
1422 break;
1423 case OP_IPOA:
1424
1425
1426
1427
1428
1429
1430 if (!altera_check_stack(stack_ptr, 2, &status))
1431 break;
1432 index = stack[--stack_ptr];
1433 count = stack[--stack_ptr];
1434
1435 if (version > 0)
1436
1437
1438
1439
1440 count = 1 + count - index;
1441
1442 charptr_tmp = (u8 *)vars[args[0]];
1443 status = altera_set_ir_post(&astate->js, count, index,
1444 charptr_tmp);
1445
1446 break;
1447 case OP_EXPT:
1448
1449
1450
1451
1452
1453 if (altera_check_stack(stack_ptr, 1, &status)) {
1454 name = &p[str_table + args[0]];
1455 long_tmp = stack[--stack_ptr];
1456 altera_export_int(name, long_tmp);
1457 }
1458 break;
1459 case OP_PSHE:
1460
1461
1462
1463
1464
1465 if (!altera_check_stack(stack_ptr, 1, &status))
1466 break;
1467 variable_id = args[0];
1468 index = stack[stack_ptr - 1];
1469
1470
1471 if ((attrs[variable_id] & 0x1f) == 0x19) {
1472
1473 longptr_tmp = (long *)vars[variable_id];
1474 stack[stack_ptr - 1] = longptr_tmp[index];
1475 } else if ((attrs[variable_id] & 0x1f) == 0x1c) {
1476
1477 long_tmp = vars[variable_id] +
1478 (index * sizeof(long));
1479 stack[stack_ptr - 1] =
1480 get_unaligned_be32(&p[long_tmp]);
1481 } else
1482 status = -ERANGE;
1483
1484 break;
1485 case OP_PSHA:
1486
1487
1488
1489
1490
1491
1492 if (!altera_check_stack(stack_ptr, 2, &status))
1493 break;
1494 variable_id = args[0];
1495
1496
1497 if ((attrs[variable_id] & 0x18) != 0x08) {
1498 status = -ERANGE;
1499 break;
1500 }
1501
1502 charptr_tmp = (u8 *)vars[variable_id];
1503
1504
1505 count = stack[--stack_ptr];
1506
1507
1508 index = stack[stack_ptr - 1];
1509
1510 if (version > 0)
1511
1512
1513
1514
1515 count = 1 + count - index;
1516
1517 if ((count < 1) || (count > 32)) {
1518 status = -ERANGE;
1519 break;
1520 }
1521
1522 long_tmp = 0L;
1523
1524 for (i = 0; i < count; ++i)
1525 if (charptr_tmp[(i + index) >> 3] &
1526 (1 << ((i + index) & 7)))
1527 long_tmp |= (1L << i);
1528
1529 stack[stack_ptr - 1] = long_tmp;
1530
1531 break;
1532 case OP_DYNA:
1533
1534
1535
1536
1537
1538 if (!altera_check_stack(stack_ptr, 1, &status))
1539 break;
1540 variable_id = args[0];
1541 long_tmp = stack[--stack_ptr];
1542
1543 if (long_tmp > var_size[variable_id]) {
1544 var_size[variable_id] = long_tmp;
1545
1546 if (attrs[variable_id] & 0x10)
1547
1548 long_tmp *= sizeof(long);
1549 else
1550
1551 long_tmp = (long_tmp + 7) >> 3;
1552
1553
1554
1555
1556
1557 if (attrs[variable_id] & 0x80) {
1558 kfree((void *)vars[variable_id]);
1559 vars[variable_id] = 0;
1560 }
1561
1562
1563
1564
1565
1566 vars[variable_id] = (long)
1567 kzalloc(long_tmp, GFP_KERNEL);
1568
1569 if (vars[variable_id] == 0) {
1570 status = -ENOMEM;
1571 break;
1572 }
1573
1574
1575
1576
1577
1578
1579 attrs[variable_id] |= 0x80;
1580
1581
1582 count = ((var_size[variable_id] + 7L) /
1583 8L);
1584 charptr_tmp = (u8 *)(vars[variable_id]);
1585 for (index = 0; index < count; ++index)
1586 charptr_tmp[index] = 0;
1587
1588 }
1589
1590 break;
1591 case OP_EXPV:
1592
1593
1594
1595
1596
1597
1598
1599 if (!altera_check_stack(stack_ptr, 3, &status))
1600 break;
1601 if (version == 0) {
1602
1603 bad_opcode = 1;
1604 break;
1605 }
1606 name = &p[str_table + args[0]];
1607 variable_id = stack[--stack_ptr];
1608 long_idx = stack[--stack_ptr];
1609 long_idx2 = stack[--stack_ptr];
1610
1611 if (long_idx > long_idx2) {
1612
1613 status = -ERANGE;
1614 break;
1615 }
1616
1617 long_count = 1 + long_idx2 - long_idx;
1618
1619 charptr_tmp = (u8 *)vars[variable_id];
1620 charptr_tmp2 = NULL;
1621
1622 if ((long_idx & 7L) != 0) {
1623 s32 k = long_idx;
1624 charptr_tmp2 =
1625 kzalloc(((long_count + 7L) / 8L),
1626 GFP_KERNEL);
1627 if (charptr_tmp2 == NULL) {
1628 status = -ENOMEM;
1629 break;
1630 }
1631
1632 for (i = 0; i < long_count; ++i) {
1633 if (charptr_tmp[k >> 3] &
1634 (1 << (k & 7)))
1635 charptr_tmp2[i >> 3] |=
1636 (1 << (i & 7));
1637 else
1638 charptr_tmp2[i >> 3] &=
1639 ~(1 << (i & 7));
1640
1641 ++k;
1642 }
1643 charptr_tmp = charptr_tmp2;
1644
1645 } else if (long_idx != 0)
1646 charptr_tmp = &charptr_tmp[long_idx >> 3];
1647
1648 altera_export_bool_array(name, charptr_tmp,
1649 long_count);
1650
1651
1652 if ((long_idx & 7L) != 0)
1653 kfree(charptr_tmp2);
1654
1655 break;
1656 case OP_COPY: {
1657
1658
1659
1660
1661
1662
1663
1664
1665 s32 copy_count;
1666 s32 copy_index;
1667 s32 copy_index2;
1668 s32 destleft;
1669 s32 src_count;
1670 s32 dest_count;
1671 int src_reverse = 0;
1672 int dest_reverse = 0;
1673
1674 if (!altera_check_stack(stack_ptr, 3, &status))
1675 break;
1676
1677 copy_count = stack[--stack_ptr];
1678 copy_index = stack[--stack_ptr];
1679 copy_index2 = stack[--stack_ptr];
1680 reverse = 0;
1681
1682 if (version > 0) {
1683
1684
1685
1686
1687
1688
1689 destleft = stack[--stack_ptr];
1690
1691 if (copy_count > copy_index) {
1692 src_reverse = 1;
1693 reverse = 1;
1694 src_count = 1 + copy_count - copy_index;
1695
1696 } else {
1697 src_count = 1 + copy_index - copy_count;
1698
1699 copy_index = copy_count;
1700 }
1701
1702 if (copy_index2 > destleft) {
1703 dest_reverse = 1;
1704 reverse = !reverse;
1705 dest_count = 1 + copy_index2 - destleft;
1706
1707 copy_index2 = destleft;
1708 } else
1709 dest_count = 1 + destleft - copy_index2;
1710
1711 copy_count = (src_count < dest_count) ?
1712 src_count : dest_count;
1713
1714 if ((src_reverse || dest_reverse) &&
1715 (src_count != dest_count))
1716
1717
1718
1719
1720
1721
1722
1723
1724 status = -ERANGE;
1725
1726 }
1727
1728 count = copy_count;
1729 index = copy_index;
1730 index2 = copy_index2;
1731
1732
1733
1734
1735
1736 variable_id = args[1];
1737 if ((version > 0) &&
1738 ((attrs[variable_id] & 0x9c) == 0x0c)) {
1739
1740 long_tmp =
1741 (var_size[variable_id] + 7L) >> 3L;
1742 charptr_tmp2 = (u8 *)vars[variable_id];
1743 charptr_tmp =
1744 kzalloc(long_tmp, GFP_KERNEL);
1745 vars[variable_id] = (long)charptr_tmp;
1746
1747 if (vars[variable_id] == 0) {
1748 status = -ENOMEM;
1749 break;
1750 }
1751
1752
1753 for (long_idx = 0L; long_idx < long_tmp;
1754 ++long_idx)
1755 charptr_tmp[long_idx] = 0;
1756
1757
1758 for (long_idx = 0L;
1759 long_idx < var_size[variable_id];
1760 ++long_idx) {
1761 long_idx2 = long_idx;
1762
1763 if (charptr_tmp2[long_idx2 >> 3] &
1764 (1 << (long_idx2 & 7)))
1765 charptr_tmp[long_idx >> 3] |=
1766 (1 << (long_idx & 7));
1767
1768 }
1769
1770
1771
1772 attrs[variable_id] |= 0x80;
1773
1774
1775 attrs[variable_id] &= ~0x04;
1776 attrs[variable_id] |= 0x01;
1777 }
1778
1779 charptr_tmp = (u8 *)vars[args[1]];
1780 charptr_tmp2 = (u8 *)vars[args[0]];
1781
1782
1783 if ((attrs[args[1]] & 0x1c) != 0x08) {
1784 status = -ERANGE;
1785 break;
1786 }
1787
1788 if (count < 1) {
1789 status = -ERANGE;
1790 break;
1791 }
1792
1793 if (reverse)
1794 index2 += (count - 1);
1795
1796 for (i = 0; i < count; ++i) {
1797 if (charptr_tmp2[index >> 3] &
1798 (1 << (index & 7)))
1799 charptr_tmp[index2 >> 3] |=
1800 (1 << (index2 & 7));
1801 else
1802 charptr_tmp[index2 >> 3] &=
1803 ~(1 << (index2 & 7));
1804
1805 ++index;
1806 if (reverse)
1807 --index2;
1808 else
1809 ++index2;
1810 }
1811
1812 break;
1813 }
1814 case OP_DSC:
1815 case OP_ISC: {
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825 s32 scan_right, scan_left;
1826 s32 capture_count = 0;
1827 s32 scan_count = 0;
1828 s32 capture_index;
1829 s32 scan_index;
1830
1831 if (!altera_check_stack(stack_ptr, 3, &status))
1832 break;
1833
1834 capture_index = stack[--stack_ptr];
1835 scan_index = stack[--stack_ptr];
1836
1837 if (version > 0) {
1838
1839
1840
1841
1842
1843
1844
1845 scan_right = stack[--stack_ptr];
1846 scan_left = stack[--stack_ptr];
1847 capture_count = 1 + scan_index - capture_index;
1848 scan_count = 1 + scan_left - scan_right;
1849 scan_index = scan_right;
1850 }
1851
1852 long_count = stack[--stack_ptr];
1853
1854
1855
1856
1857 variable_id = args[1];
1858 if ((version > 0) &&
1859 ((attrs[variable_id] & 0x9c) == 0x0c)) {
1860
1861 long_tmp =
1862 (var_size[variable_id] + 7L) >> 3L;
1863 charptr_tmp2 = (u8 *)vars[variable_id];
1864 charptr_tmp =
1865 kzalloc(long_tmp, GFP_KERNEL);
1866 vars[variable_id] = (long)charptr_tmp;
1867
1868 if (vars[variable_id] == 0) {
1869 status = -ENOMEM;
1870 break;
1871 }
1872
1873
1874 for (long_idx = 0L; long_idx < long_tmp;
1875 ++long_idx)
1876 charptr_tmp[long_idx] = 0;
1877
1878
1879 for (long_idx = 0L;
1880 long_idx < var_size[variable_id];
1881 ++long_idx) {
1882 long_idx2 = long_idx;
1883
1884 if (charptr_tmp2[long_idx2 >> 3] &
1885 (1 << (long_idx2 & 7)))
1886 charptr_tmp[long_idx >> 3] |=
1887 (1 << (long_idx & 7));
1888
1889 }
1890
1891
1892
1893
1894
1895 attrs[variable_id] |= 0x80;
1896
1897
1898 attrs[variable_id] &= ~0x04;
1899 attrs[variable_id] |= 0x01;
1900
1901 }
1902
1903 charptr_tmp = (u8 *)vars[args[0]];
1904 charptr_tmp2 = (u8 *)vars[args[1]];
1905
1906 if ((version > 0) &&
1907 ((long_count > capture_count) ||
1908 (long_count > scan_count))) {
1909 status = -ERANGE;
1910 break;
1911 }
1912
1913
1914
1915
1916
1917 if ((attrs[args[1]] & 0x1c) != 0x08) {
1918 status = -ERANGE;
1919 break;
1920 }
1921
1922 if (status == 0) {
1923 if (opcode == 0x82)
1924 status = altera_swap_dr(astate,
1925 long_count,
1926 charptr_tmp,
1927 scan_index,
1928 charptr_tmp2,
1929 capture_index);
1930 else
1931 status = altera_swap_ir(astate,
1932 long_count,
1933 charptr_tmp,
1934 scan_index,
1935 charptr_tmp2,
1936 capture_index);
1937
1938 }
1939
1940 break;
1941 }
1942 case OP_WAIT:
1943
1944
1945
1946
1947
1948
1949
1950 if (!altera_check_stack(stack_ptr, 2, &status))
1951 break;
1952 long_tmp = stack[--stack_ptr];
1953
1954 if (long_tmp != 0L)
1955 status = altera_wait_cycles(astate, long_tmp,
1956 args[0]);
1957
1958 long_tmp = stack[--stack_ptr];
1959
1960 if ((status == 0) && (long_tmp != 0L))
1961 status = altera_wait_msecs(astate,
1962 long_tmp,
1963 args[0]);
1964
1965 if ((status == 0) && (args[1] != args[0]))
1966 status = altera_goto_jstate(astate,
1967 args[1]);
1968
1969 if (version > 0) {
1970 --stack_ptr;
1971 --stack_ptr;
1972 }
1973 break;
1974 case OP_CMPA: {
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985 s32 a, b;
1986 u8 *source1 = (u8 *)vars[args[0]];
1987 u8 *source2 = (u8 *)vars[args[1]];
1988 u8 *mask = (u8 *)vars[args[2]];
1989 u32 index1;
1990 u32 index2;
1991 u32 mask_index;
1992
1993 if (!altera_check_stack(stack_ptr, 4, &status))
1994 break;
1995
1996 index1 = stack[--stack_ptr];
1997 index2 = stack[--stack_ptr];
1998 mask_index = stack[--stack_ptr];
1999 long_count = stack[--stack_ptr];
2000
2001 if (version > 0) {
2002
2003
2004
2005
2006
2007
2008
2009
2010 s32 mask_right = stack[--stack_ptr];
2011 s32 mask_left = stack[--stack_ptr];
2012
2013 a = 1 + index2 - index1;
2014
2015 b = 1 + long_count - mask_index;
2016 a = (a < b) ? a : b;
2017
2018 b = 1 + mask_left - mask_right;
2019 a = (a < b) ? a : b;
2020
2021 index2 = mask_index;
2022
2023 mask_index = mask_right;
2024 long_count = a;
2025 }
2026
2027 long_tmp = 1L;
2028
2029 if (long_count < 1)
2030 status = -ERANGE;
2031 else {
2032 count = long_count;
2033
2034 for (i = 0; i < count; ++i) {
2035 if (mask[mask_index >> 3] &
2036 (1 << (mask_index & 7))) {
2037 a = source1[index1 >> 3] &
2038 (1 << (index1 & 7))
2039 ? 1 : 0;
2040 b = source2[index2 >> 3] &
2041 (1 << (index2 & 7))
2042 ? 1 : 0;
2043
2044 if (a != b)
2045 long_tmp = 0L;
2046 }
2047 ++index1;
2048 ++index2;
2049 ++mask_index;
2050 }
2051 }
2052
2053 stack[stack_ptr++] = long_tmp;
2054
2055 break;
2056 }
2057 default:
2058
2059 bad_opcode = 1;
2060 break;
2061 }
2062
2063 if (bad_opcode)
2064 status = -ENOSYS;
2065
2066 if ((stack_ptr < 0) || (stack_ptr >= ALTERA_STACK_SIZE))
2067 status = -EOVERFLOW;
2068
2069 if (status != 0) {
2070 done = 1;
2071 *error_address = (s32)(opcode_address - code_sect);
2072 }
2073 }
2074
2075 altera_free_buffers(astate);
2076
2077
2078 if ((attrs != NULL) && (vars != NULL))
2079 for (i = 0; i < sym_count; ++i)
2080 if (attrs[i] & 0x80)
2081 kfree((void *)vars[i]);
2082
2083 kfree(vars);
2084 kfree(var_size);
2085 kfree(attrs);
2086 kfree(proc_attributes);
2087
2088 return status;
2089 }
2090
2091 static int altera_get_note(u8 *p, s32 program_size, s32 *offset,
2092 char *key, char *value, int keylen, int vallen)
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102 {
2103 int status = -ENODATA;
2104 u32 note_strings = 0L;
2105 u32 note_table = 0L;
2106 u32 note_count = 0L;
2107 u32 first_word = 0L;
2108 int version = 0;
2109 int delta = 0;
2110 char *key_ptr;
2111 char *value_ptr;
2112 int i;
2113
2114
2115 if (program_size > 52L) {
2116 first_word = get_unaligned_be32(&p[0]);
2117 version = (first_word & 1L);
2118 delta = version * 8;
2119
2120 note_strings = get_unaligned_be32(&p[8 + delta]);
2121 note_table = get_unaligned_be32(&p[12 + delta]);
2122 note_count = get_unaligned_be32(&p[44 + (2 * delta)]);
2123 }
2124
2125 if ((first_word != 0x4A414D00L) && (first_word != 0x4A414D01L))
2126 return -EIO;
2127
2128 if (note_count <= 0L)
2129 return status;
2130
2131 if (offset == NULL) {
2132
2133
2134
2135
2136 for (i = 0; (i < note_count) &&
2137 (status != 0); ++i) {
2138 key_ptr = &p[note_strings +
2139 get_unaligned_be32(
2140 &p[note_table + (8 * i)])];
2141 if (key && !strncasecmp(key, key_ptr, strlen(key_ptr))) {
2142 status = 0;
2143
2144 value_ptr = &p[note_strings +
2145 get_unaligned_be32(
2146 &p[note_table + (8 * i) + 4])];
2147
2148 if (value != NULL)
2149 strlcpy(value, value_ptr, vallen);
2150
2151 }
2152 }
2153 } else {
2154
2155
2156
2157
2158
2159 i = *offset;
2160
2161 if ((i >= 0) && (i < note_count)) {
2162 status = 0;
2163
2164 if (key != NULL)
2165 strlcpy(key, &p[note_strings +
2166 get_unaligned_be32(
2167 &p[note_table + (8 * i)])],
2168 keylen);
2169
2170 if (value != NULL)
2171 strlcpy(value, &p[note_strings +
2172 get_unaligned_be32(
2173 &p[note_table + (8 * i) + 4])],
2174 vallen);
2175
2176 *offset = i + 1;
2177 }
2178 }
2179
2180 return status;
2181 }
2182
2183 static int altera_check_crc(u8 *p, s32 program_size)
2184 {
2185 int status = 0;
2186 u16 local_expected = 0,
2187 local_actual = 0,
2188 shift_reg = 0xffff;
2189 int bit, feedback;
2190 u8 databyte;
2191 u32 i;
2192 u32 crc_section = 0L;
2193 u32 first_word = 0L;
2194 int version = 0;
2195 int delta = 0;
2196
2197 if (program_size > 52L) {
2198 first_word = get_unaligned_be32(&p[0]);
2199 version = (first_word & 1L);
2200 delta = version * 8;
2201
2202 crc_section = get_unaligned_be32(&p[32 + delta]);
2203 }
2204
2205 if ((first_word != 0x4A414D00L) && (first_word != 0x4A414D01L))
2206 status = -EIO;
2207
2208 if (crc_section >= program_size)
2209 status = -EIO;
2210
2211 if (status == 0) {
2212 local_expected = (u16)get_unaligned_be16(&p[crc_section]);
2213
2214 for (i = 0; i < crc_section; ++i) {
2215 databyte = p[i];
2216 for (bit = 0; bit < 8; bit++) {
2217 feedback = (databyte ^ shift_reg) & 0x01;
2218 shift_reg >>= 1;
2219 if (feedback)
2220 shift_reg ^= 0x8408;
2221
2222 databyte >>= 1;
2223 }
2224 }
2225
2226 local_actual = (u16)~shift_reg;
2227
2228 if (local_expected != local_actual)
2229 status = -EILSEQ;
2230
2231 }
2232
2233 if (debug || status) {
2234 switch (status) {
2235 case 0:
2236 printk(KERN_INFO "%s: CRC matched: %04x\n", __func__,
2237 local_actual);
2238 break;
2239 case -EILSEQ:
2240 printk(KERN_ERR "%s: CRC mismatch: expected %04x, "
2241 "actual %04x\n", __func__, local_expected,
2242 local_actual);
2243 break;
2244 case -EIO:
2245 printk(KERN_ERR "%s: error: format isn't "
2246 "recognized.\n", __func__);
2247 break;
2248 default:
2249 printk(KERN_ERR "%s: CRC function returned error "
2250 "code %d\n", __func__, status);
2251 break;
2252 }
2253 }
2254
2255 return status;
2256 }
2257
2258 static int altera_get_file_info(u8 *p,
2259 s32 program_size,
2260 int *format_version,
2261 int *action_count,
2262 int *procedure_count)
2263 {
2264 int status = -EIO;
2265 u32 first_word = 0;
2266 int version = 0;
2267
2268 if (program_size <= 52L)
2269 return status;
2270
2271 first_word = get_unaligned_be32(&p[0]);
2272
2273 if ((first_word == 0x4A414D00L) || (first_word == 0x4A414D01L)) {
2274 status = 0;
2275
2276 version = (first_word & 1L);
2277 *format_version = version + 1;
2278
2279 if (version > 0) {
2280 *action_count = get_unaligned_be32(&p[48]);
2281 *procedure_count = get_unaligned_be32(&p[52]);
2282 }
2283 }
2284
2285 return status;
2286 }
2287
2288 static int altera_get_act_info(u8 *p,
2289 s32 program_size,
2290 int index,
2291 char **name,
2292 char **description,
2293 struct altera_procinfo **proc_list)
2294 {
2295 int status = -EIO;
2296 struct altera_procinfo *procptr = NULL;
2297 struct altera_procinfo *tmpptr = NULL;
2298 u32 first_word = 0L;
2299 u32 action_table = 0L;
2300 u32 proc_table = 0L;
2301 u32 str_table = 0L;
2302 u32 note_strings = 0L;
2303 u32 action_count = 0L;
2304 u32 proc_count = 0L;
2305 u32 act_name_id = 0L;
2306 u32 act_desc_id = 0L;
2307 u32 act_proc_id = 0L;
2308 u32 act_proc_name = 0L;
2309 u8 act_proc_attribute = 0;
2310
2311 if (program_size <= 52L)
2312 return status;
2313
2314 first_word = get_unaligned_be32(&p[0]);
2315
2316 if (first_word != 0x4A414D01L)
2317 return status;
2318
2319 action_table = get_unaligned_be32(&p[4]);
2320 proc_table = get_unaligned_be32(&p[8]);
2321 str_table = get_unaligned_be32(&p[12]);
2322 note_strings = get_unaligned_be32(&p[16]);
2323 action_count = get_unaligned_be32(&p[48]);
2324 proc_count = get_unaligned_be32(&p[52]);
2325
2326 if (index >= action_count)
2327 return status;
2328
2329 act_name_id = get_unaligned_be32(&p[action_table + (12 * index)]);
2330 act_desc_id = get_unaligned_be32(&p[action_table + (12 * index) + 4]);
2331 act_proc_id = get_unaligned_be32(&p[action_table + (12 * index) + 8]);
2332
2333 *name = &p[str_table + act_name_id];
2334
2335 if (act_desc_id < (note_strings - str_table))
2336 *description = &p[str_table + act_desc_id];
2337
2338 do {
2339 act_proc_name = get_unaligned_be32(
2340 &p[proc_table + (13 * act_proc_id)]);
2341 act_proc_attribute =
2342 (p[proc_table + (13 * act_proc_id) + 8] & 0x03);
2343
2344 procptr =
2345 kzalloc(sizeof(struct altera_procinfo),
2346 GFP_KERNEL);
2347
2348 if (procptr == NULL)
2349 status = -ENOMEM;
2350 else {
2351 procptr->name = &p[str_table + act_proc_name];
2352 procptr->attrs = act_proc_attribute;
2353 procptr->next = NULL;
2354
2355
2356 if (*proc_list == NULL)
2357 *proc_list = procptr;
2358 else {
2359 tmpptr = *proc_list;
2360 while (tmpptr->next != NULL)
2361 tmpptr = tmpptr->next;
2362 tmpptr->next = procptr;
2363 }
2364 }
2365
2366 act_proc_id = get_unaligned_be32(
2367 &p[proc_table + (13 * act_proc_id) + 4]);
2368 } while ((act_proc_id != 0) && (act_proc_id < proc_count));
2369
2370 return status;
2371 }
2372
2373 int altera_init(struct altera_config *config, const struct firmware *fw)
2374 {
2375 struct altera_state *astate = NULL;
2376 struct altera_procinfo *proc_list = NULL;
2377 struct altera_procinfo *procptr = NULL;
2378 char *key = NULL;
2379 char *value = NULL;
2380 char *action_name = NULL;
2381 char *description = NULL;
2382 int exec_result = 0;
2383 int exit_code = 0;
2384 int format_version = 0;
2385 int action_count = 0;
2386 int procedure_count = 0;
2387 int index = 0;
2388 s32 offset = 0L;
2389 s32 error_address = 0L;
2390 int retval = 0;
2391
2392 key = kzalloc(33, GFP_KERNEL);
2393 if (!key) {
2394 retval = -ENOMEM;
2395 goto out;
2396 }
2397 value = kzalloc(257, GFP_KERNEL);
2398 if (!value) {
2399 retval = -ENOMEM;
2400 goto free_key;
2401 }
2402 astate = kzalloc(sizeof(struct altera_state), GFP_KERNEL);
2403 if (!astate) {
2404 retval = -ENOMEM;
2405 goto free_value;
2406 }
2407
2408 astate->config = config;
2409 if (!astate->config->jtag_io) {
2410 dprintk("%s: using byteblaster!\n", __func__);
2411 astate->config->jtag_io = netup_jtag_io_lpt;
2412 }
2413
2414 altera_check_crc((u8 *)fw->data, fw->size);
2415
2416 if (debug) {
2417 altera_get_file_info((u8 *)fw->data, fw->size, &format_version,
2418 &action_count, &procedure_count);
2419 printk(KERN_INFO "%s: File format is %s ByteCode format\n",
2420 __func__, (format_version == 2) ? "Jam STAPL" :
2421 "pre-standardized Jam 1.1");
2422 while (altera_get_note((u8 *)fw->data, fw->size,
2423 &offset, key, value, 32, 256) == 0)
2424 printk(KERN_INFO "%s: NOTE \"%s\" = \"%s\"\n",
2425 __func__, key, value);
2426 }
2427
2428 if (debug && (format_version == 2) && (action_count > 0)) {
2429 printk(KERN_INFO "%s: Actions available:\n", __func__);
2430 for (index = 0; index < action_count; ++index) {
2431 altera_get_act_info((u8 *)fw->data, fw->size,
2432 index, &action_name,
2433 &description,
2434 &proc_list);
2435
2436 if (description == NULL)
2437 printk(KERN_INFO "%s: %s\n",
2438 __func__,
2439 action_name);
2440 else
2441 printk(KERN_INFO "%s: %s \"%s\"\n",
2442 __func__,
2443 action_name,
2444 description);
2445
2446 procptr = proc_list;
2447 while (procptr != NULL) {
2448 if (procptr->attrs != 0)
2449 printk(KERN_INFO "%s: %s (%s)\n",
2450 __func__,
2451 procptr->name,
2452 (procptr->attrs == 1) ?
2453 "optional" : "recommended");
2454
2455 proc_list = procptr->next;
2456 kfree(procptr);
2457 procptr = proc_list;
2458 }
2459 }
2460
2461 printk(KERN_INFO "\n");
2462 }
2463
2464 exec_result = altera_execute(astate, (u8 *)fw->data, fw->size,
2465 &error_address, &exit_code, &format_version);
2466
2467 if (exit_code)
2468 exec_result = -EREMOTEIO;
2469
2470 if ((format_version == 2) && (exec_result == -EINVAL)) {
2471 if (astate->config->action == NULL)
2472 printk(KERN_ERR "%s: error: no action specified for "
2473 "Jam STAPL file.\nprogram terminated.\n",
2474 __func__);
2475 else
2476 printk(KERN_ERR "%s: error: action \"%s\""
2477 " is not supported "
2478 "for this Jam STAPL file.\n"
2479 "Program terminated.\n", __func__,
2480 astate->config->action);
2481
2482 } else if (exec_result)
2483 printk(KERN_ERR "%s: error %d\n", __func__, exec_result);
2484
2485 kfree(astate);
2486 free_value:
2487 kfree(value);
2488 free_key:
2489 kfree(key);
2490 out:
2491 return retval;
2492 }
2493 EXPORT_SYMBOL(altera_init);