/** * Use the Chinese Remainder Theorem to quickly perform RSA decrypts. * This should really be in bigint.c (and was at one stage), but needs * access to the RSA_CTX context... */ static bigint *bi_crt(const RSA_CTX *rsa, bigint *bi) { BI_CTX *ctx = rsa->bi_ctx; bigint *m1, *m2, *h; /* Montgomery has a condition the 0 < x, y < m and these products violate * that condition. So disable Montgomery when using CRT */ #if defined(CONFIG_BIGINT_MONTGOMERY) ctx->use_classical = 1; #endif ctx->mod_offset = BIGINT_P_OFFSET; m1 = bi_mod_power(ctx, bi_copy(bi), rsa->dP); ctx->mod_offset = BIGINT_Q_OFFSET; m2 = bi_mod_power(ctx, bi, rsa->dQ); h = bi_subtract(ctx, bi_add(ctx, m1, rsa->p), bi_copy(m2), NULL); h = bi_multiply(ctx, h, rsa->qInv); ctx->mod_offset = BIGINT_P_OFFSET; h = bi_residue(ctx, h); #if defined(CONFIG_BIGINT_MONTGOMERY) ctx->use_classical = 0; /* reset for any further operation */ #endif return bi_add(ctx, m2, bi_multiply(ctx, rsa->q, h)); }
/** * @brief Use the Chinese Remainder Theorem to quickly perform RSA decrypts. * * @param ctx [in] The bigint session context. * @param bi [in] The bigint to perform the exp/mod. * @param dP [in] CRT's dP bigint * @param dQ [in] CRT's dQ bigint * @param p [in] CRT's p bigint * @param q [in] CRT's q bigint * @param qInv [in] CRT's qInv bigint * @return The result of the CRT operation */ bigint * ICACHE_FLASH_ATTR bi_crt(BI_CTX *ctx, bigint *bi, bigint *dP, bigint *dQ, bigint *p, bigint *q, bigint *qInv) { bigint *m1, *m2, *h; /* Montgomery has a condition the 0 < x, y < m and these products violate * that condition. So disable Montgomery when using CRT */ #if defined(CONFIG_BIGINT_MONTGOMERY) ctx->use_classical = 1; #endif ctx->mod_offset = BIGINT_P_OFFSET; m1 = bi_mod_power(ctx, bi_copy(bi), dP); ctx->mod_offset = BIGINT_Q_OFFSET; m2 = bi_mod_power(ctx, bi, dQ); h = bi_subtract(ctx, bi_add(ctx, m1, p), bi_copy(m2), NULL); h = bi_multiply(ctx, h, qInv); ctx->mod_offset = BIGINT_P_OFFSET; h = bi_residue(ctx, h); #if defined(CONFIG_BIGINT_MONTGOMERY) ctx->use_classical = 0; /* reset for any further operation */ #endif return bi_add(ctx, m2, bi_multiply(ctx, q, h)); }
int test_add_two_size() { big_int a= bi_init("1234"); big_int b= bi_init("9876543"); if (0 == strcmp(bi_get(bi_add(a,b)), "9877777")) { return 1; } printf("\nUnexpected Value: %s\n", bi_get(bi_add(a,b))); return 0; }
int test_add_double() { big_int a= bi_init("99"); big_int b= bi_init("1"); if (0 == strcmp(bi_get(bi_add(a,b)), "100")) { return 1; } printf("\nUnexpected Value: %s\n", bi_get(bi_add(a,b))); return 0; }
int test_add_single() { big_int a= bi_init("3"); big_int b= bi_init("4"); if (0 == strcmp(bi_get(bi_add(a,b)), "7")) { return 1; } printf("\nUnexpected Value: %s\n", bi_get(bi_add(a,b))); return 0; }
/* * Karatsuba improves on regular multiplication due to only 3 multiplications * being done instead of 4. The additional additions/subtractions are O(N) * rather than O(N^2) and so for big numbers it saves on a few operations */ static bigint * ICACHE_FLASH_ATTR karatsuba(BI_CTX *ctx, bigint *bia, bigint *bib, int is_square) { bigint *x0, *x1; bigint *p0, *p1, *p2; int m; if (is_square) { m = (bia->size + 1)/2; } else { m = (max(bia->size, bib->size) + 1)/2; } x0 = bi_clone(ctx, bia); x0->size = m; x1 = bi_clone(ctx, bia); comp_right_shift(x1, m); bi_free(ctx, bia); /* work out the 3 partial products */ if (is_square) { p0 = bi_square(ctx, bi_copy(x0)); p2 = bi_square(ctx, bi_copy(x1)); p1 = bi_square(ctx, bi_add(ctx, x0, x1)); } else /* normal multiply */ { bigint *y0, *y1; y0 = bi_clone(ctx, bib); y0->size = m; y1 = bi_clone(ctx, bib); comp_right_shift(y1, m); bi_free(ctx, bib); p0 = bi_multiply(ctx, bi_copy(x0), bi_copy(y0)); p2 = bi_multiply(ctx, bi_copy(x1), bi_copy(y1)); p1 = bi_multiply(ctx, bi_add(ctx, x0, x1), bi_add(ctx, y0, y1)); } p1 = bi_subtract(ctx, bi_subtract(ctx, p1, bi_copy(p2), NULL), bi_copy(p0), NULL); comp_left_shift(p1, m); comp_left_shift(p2, 2*m); return bi_add(ctx, p1, bi_add(ctx, p0, p2)); }
int main() { bi_initialize(); int k = 2; bigint f1 = int_to_bi(1), f2 = int_to_bi(1), f3, tmp; bigint zeros = int_to_bi(1000000000); while (1) { k++; f3 = bi_add(f1, bi_copy(f2)); int mod = bi_int_mod(bi_copy(f3), 1000000000); if (mod >= 100000000) { if (is_pandigital(mod)) { tmp = bi_copy(f3); while (bi_compare(bi_copy(tmp), bi_copy(zeros)) > 0) { tmp = bi_int_divide(tmp, 10); } if (is_pandigital(bi_to_int(tmp))) { break; } } } f1 = f2; f2 = f3; } bi_free(f2); bi_free(f3); bi_free(zeros); printf("%d\n", k); bi_terminate(); return 0; }
/** * @brief Perform a single montgomery reduction. * @param ctx [in] The bigint session context. * @param bixy [in] A bigint. * @return The result of the montgomery reduction. */ bigint * ICACHE_FLASH_ATTR bi_mont(BI_CTX *ctx, bigint *bixy) { int i = 0, n; uint8_t mod_offset = ctx->mod_offset; bigint *bim = ctx->bi_mod[mod_offset]; comp mod_inv = ctx->N0_dash[mod_offset]; check(bixy); if (ctx->use_classical) /* just use classical instead */ { return bi_mod(ctx, bixy); } n = bim->size; do { bixy = bi_add(ctx, bixy, comp_left_shift( bi_int_multiply(ctx, bim, bixy->comps[i]*mod_inv), i)); } while (++i < n); comp_right_shift(bixy, n); if (bi_compare(bixy, bim) >= 0) { bixy = bi_subtract(ctx, bixy, bim, NULL); } return bixy; }
int main() { bi_initialize(); bigint sum = int_to_bi(1); bigint last = int_to_bi(1); int i, j; for (i = 1; i <= (SIZE-1)/2; ++i) { for (j = 0; j < 4; ++j) { last = bi_add(last, bi_int_multiply(int_to_bi(i), 2)); sum = bi_add(sum, bi_copy(last)); } } bi_print(stdout, sum); printf("\n"); bi_free(last); bi_terminate(); return 0; }
void bi_init(void *addr) { struct btinfo_magic bi_magic; memset(addr, 0, BOOTINFO_MAXSIZE); bi_next = (char *)addr; bi_size = 0; bi_magic.magic = BOOTINFO_MAGIC; bi_add(&bi_magic, BTINFO_MAGIC, sizeof(bi_magic)); }
int main() { bi_initialize(); int a, b, n; int max_number, prod; for (a = -999; a < 1000; ++a) { for (b = -999; b < 1000; ++b) { n = 0; for (;;) { bigint a_ = int_to_bi(a); bigint b_ = int_to_bi(b); bigint n_ = int_to_bi(n); bigint n2 = bi_multiply( bi_copy( n_ ), bi_copy( n_ ) ); bigint an = bi_multiply(bi_copy(a_),bi_copy(n_)); bigint num = bi_add(bi_copy(n2), bi_copy(an)); bigint num2 = bi_add(bi_copy(num), bi_copy(b_)); int should_break = 0; if (bi_is_probable_prime(bi_copy(num2), 99)) { ++n; } else { if (n > max_number) { max_number = n; prod = a * b; } should_break = 1; } bi_free(num); bi_free(num2); bi_free(a_); bi_free(b_); bi_free(n_); bi_free(n2); bi_free(an); if (should_break) break; } } } printf("%d\n", prod); bi_terminate(); return 0; }
void bi_init(paddr_t addr) { struct btinfo_common *bi; struct btinfo_magic bi_magic; bootinfo = (char *)addr; bi = (struct btinfo_common *)bootinfo; bi->next = bi->type = 0; bi_next = bootinfo; bi_size = 0; bi_magic.magic = BOOTINFO_MAGIC; bi_add(&bi_magic, BTINFO_MAGIC, sizeof(bi_magic)); }
void bi_init(void *bi_addr) { struct btinfo_magic bi_magic; bootinfo = bi_addr; bootinfo->next = 0; bootinfo->type = BTINFO_NONE; bi_next = (void *)bootinfo; bi_size = 0; bi_magic.magic = BOOTINFO_MAGIC; bi_add(&bi_magic, BTINFO_MAGIC, sizeof(bi_magic)); }
int main() { bi_initialize(); bigint last = int_to_bi(1); int i, j; int number_of_primes = 0; for (i = 1;; ++i) { for (j = 0; j < 4; ++j) { last = bi_add(last, bi_int_multiply(int_to_bi(i), 2)); if (j != 3 && bi_is_probable_prime(bi_copy(last), 4)) { ++number_of_primes; } } if ((double)number_of_primes / (4*i+1) < 0.1) { printf("%d\n", i*2+1); break; } #ifdef DEBUG printf("%d/%d on iteration %d ~%f\n", number_of_primes, (4*i+1), i, (float)number_of_primes/(4*i+1)); #endif } bi_free(last); bi_terminate(); return 0; }
/* * Prepare boot information and jump directly to the kernel. */ static void jump_to_kernel(u_long *marks, char *kernel, char *args, void *ofw, int boothowto) { int l, machine_tag; long newargs[4]; void *ssym, *esym; vaddr_t bootinfo; struct btinfo_symtab bi_sym; struct btinfo_kernend bi_kend; struct btinfo_boothowto bi_howto; char *cp; char bootline[PROM_MAX_PATH * 2]; /* Compose kernel boot line. */ strncpy(bootline, kernel, sizeof(bootline)); cp = bootline + strlen(bootline); if (*args) { *cp++ = ' '; strncpy(bootline, args, sizeof(bootline) - (cp - bootline)); } *cp = 0; args = bootline; /* Record symbol information in the bootinfo. */ bootinfo = bi_init(marks[MARK_END]); bi_sym.nsym = marks[MARK_NSYM]; bi_sym.ssym = marks[MARK_SYM]; bi_sym.esym = marks[MARK_END]; bi_add(&bi_sym, BTINFO_SYMTAB, sizeof(bi_sym)); bi_kend.addr= bootinfo + BOOTINFO_SIZE; bi_add(&bi_kend, BTINFO_KERNEND, sizeof(bi_kend)); bi_howto.boothowto = boothowto; bi_add(&bi_howto, BTINFO_BOOTHOWTO, sizeof(bi_howto)); if (bootinfo_pass_bootdev) { struct { struct btinfo_common common; char name[256]; } info; strcpy(info.name, bootdev); bi_add(&info, BTINFO_BOOTDEV, strlen(bootdev) +sizeof(struct btinfo_bootdev)); } sparc64_finalize_tlb(marks[MARK_DATA]); sparc64_bi_add(); ssym = (void*)(long)marks[MARK_SYM]; esym = (void*)(long)marks[MARK_END]; DPRINTF(("jump_to_kernel(): ssym = %p, esym = %p\n", ssym, esym)); /* Adjust ksyms pointers, if needed. */ if (COMPAT_BOOT(marks) || compatmode) { ksyms_copyout(&ssym, &esym); } freeall(); /* * When we come in args consists of a pointer to the boot * string. We need to fix it so it takes into account * other params such as romp. */ /* * Stash pointer to end of symbol table after the argument * strings. */ l = strlen(args) + 1; memcpy(args + l, &esym, sizeof(esym)); l += sizeof(esym); /* * Tell the kernel we're an OpenFirmware system. */ machine_tag = SPARC_MACHINE_OPENFIRMWARE; memcpy(args + l, &machine_tag, sizeof(machine_tag)); l += sizeof(machine_tag); /* * Since we don't need the boot string (we can get it from /chosen) * we won't pass it in. Just pass in esym and magic # */ newargs[0] = SPARC_MACHINE_OPENFIRMWARE; newargs[1] = (long)esym; newargs[2] = (long)ssym; newargs[3] = (long)(void*)bootinfo; args = (char *)newargs; l = sizeof(newargs); /* if -D is set then pause in the PROM. */ if (debug > 1) callrom(); /* * Jump directly to the kernel. Solaris kernel and Sun PROM * flash updates expect ROMP vector in %o0, so we do. Format * of other parameters and their order reflect OF_chain() * symantics since this is what older NetBSD kernels rely on. * (see sparc64/include/bootinfo.h for specification). */ DPRINTF(("jump_to_kernel(%lx, %lx, %lx, %lx, %lx) @ %p\n", (long)ofw, (long)args, (long)l, (long)ofw, (long)ofw, (void*)marks[MARK_ENTRY])); (*(entry_t)marks[MARK_ENTRY])((long)ofw, (long)args, (long)l, (long)ofw, (long)ofw); printf("Returned from kernel entry point!\n"); }
void main(int argc, char *argv[], char *bootargs_start, char *bootargs_end) { unsigned long marks[MARK_MAX]; struct brdprop *brdprop; char *new_argv[MAX_ARGS]; char *bname; ssize_t len; int err, fd, howto, i, n; printf("\n>> %s altboot, revision %s\n", bootprog_name, bootprog_rev); brdprop = brd_lookup(brdtype); printf(">> %s, cpu %u MHz, bus %u MHz, %dMB SDRAM\n", brdprop->verbose, cpuclock / 1000000, busclock / 1000000, bi_mem.memsize >> 20); nata = pcilookup(PCI_CLASS_IDE, lata, 2); if (nata == 0) nata = pcilookup(PCI_CLASS_RAID, lata, 2); if (nata == 0) nata = pcilookup(PCI_CLASS_MISCSTORAGE, lata, 2); if (nata == 0) nata = pcilookup(PCI_CLASS_SCSI, lata, 2); nnif = pcilookup(PCI_CLASS_ETH, lnif, 2); nusb = pcilookup(PCI_CLASS_USB, lusb, 3); #ifdef DEBUG if (nata == 0) printf("No IDE/SATA found\n"); else for (n = 0; n < nata; n++) { int b, d, f, bdf, pvd; bdf = lata[n].bdf; pvd = lata[n].pvd; pcidecomposetag(bdf, &b, &d, &f); printf("%04x.%04x DSK %02d:%02d:%02d\n", PCI_VENDOR(pvd), PCI_PRODUCT(pvd), b, d, f); } if (nnif == 0) printf("no NET found\n"); else for (n = 0; n < nnif; n++) { int b, d, f, bdf, pvd; bdf = lnif[n].bdf; pvd = lnif[n].pvd; pcidecomposetag(bdf, &b, &d, &f); printf("%04x.%04x NET %02d:%02d:%02d\n", PCI_VENDOR(pvd), PCI_PRODUCT(pvd), b, d, f); } if (nusb == 0) printf("no USB found\n"); else for (n = 0; n < nusb; n++) { int b, d, f, bdf, pvd; bdf = lusb[0].bdf; pvd = lusb[0].pvd; pcidecomposetag(bdf, &b, &d, &f); printf("%04x.%04x USB %02d:%02d:%02d\n", PCI_VENDOR(pvd), PCI_PRODUCT(pvd), b, d, f); } #endif pcisetup(); pcifixup(); /* * When argc is too big then it is probably a pointer, which could * indicate that we were launched as a Linux kernel module using * "bootm". */ if (argc > MAX_ARGS) { if (argv != NULL) { /* * initrd image was loaded: * check if it contains a valid altboot command line */ char *p = (char *)argv; if (strncmp(p, "altboot:", 8) == 0) { *p = 0; for (p = p + 8; *p >= ' '; p++); argc = parse_cmdline(new_argv, MAX_ARGS, ((char *)argv) + 8, p); argv = new_argv; } else argc = 0; /* boot default */ } else { /* parse standard Linux bootargs */ argc = parse_cmdline(new_argv, MAX_ARGS, bootargs_start, bootargs_end); argv = new_argv; } } /* look for a PATA drive configuration string under the arguments */ for (n = 1; n < argc; n++) { if (strncmp(argv[n], "ide:", 4) == 0 && argv[n][4] >= '0' && argv[n][4] <= '2') { drive_config = &argv[n][4]; break; } } /* intialize a disk driver */ for (i = 0, n = 0; i < nata; i++) n += dskdv_init(&lata[i]); if (n == 0) printf("IDE/SATA device driver was not found\n"); /* initialize a network interface */ for (n = 0; n < nnif; n++) if (netif_init(&lnif[n]) != 0) break; if (n >= nnif) printf("no NET device driver was found\n"); /* wait 2s for user to enter interactive mode */ for (n = 200; n >= 0; n--) { if (n % 100 == 0) printf("\rHit any key to enter interactive mode: %d", n / 100); if (tstchar()) { #ifdef DEBUG unsigned c; c = toupper(getchar()); if (c == 'C') { /* controller test terminal */ sat_test(); n = 200; continue; } else if (c == 'F') { /* find strings in Flash ROM */ findflash(); n = 200; continue; } #else (void)getchar(); #endif /* enter command line */ argv = new_argv; argc = input_cmdline(argv, MAX_ARGS); break; } delay(10000); } putchar('\n'); howto = RB_AUTOBOOT; /* default is autoboot = 0 */ /* get boot options and determine bootname */ for (n = 1; n < argc; n++) { if (strncmp(argv[n], "ide:", 4) == 0) continue; /* ignore drive configuration argument */ for (i = 0; i < sizeof(bootargs) / sizeof(bootargs[0]); i++) { if (strncasecmp(argv[n], bootargs[i].name, strlen(bootargs[i].name)) == 0) { howto |= bootargs[i].value; break; } } if (i >= sizeof(bootargs) / sizeof(bootargs[0])) break; /* break on first unknown string */ } /* * If no device name is given, we construct a list of drives * which have valid disklabels. */ if (n >= argc) { static const size_t blen = sizeof("wdN:"); n = 0; argc = 0; argv = alloc(MAX_UNITS * (sizeof(char *) + blen)); bname = (char *)(argv + MAX_UNITS); for (i = 0; i < MAX_UNITS; i++) { if (!dlabel_valid(i)) continue; snprintf(bname, blen, "wd%d:", i); argv[argc++] = bname; bname += blen; } /* use default drive if no valid disklabel is found */ if (argc == 0) { argc = 1; argv[0] = BNAME_DEFAULT; } } /* try to boot off kernel from the drive list */ while (n < argc) { bname = argv[n++]; if (check_bootname(bname) == 0) { printf("%s not a valid bootname\n", bname); continue; } if ((fd = open(bname, 0)) < 0) { if (errno == ENOENT) printf("\"%s\" not found\n", bi_path.bootpath); continue; } printf("loading \"%s\" ", bi_path.bootpath); marks[MARK_START] = 0; if (howto == -1) { /* load another altboot binary and replace ourselves */ len = read(fd, (void *)0x100000, 0x1000000 - 0x100000); if (len == -1) goto loadfail; close(fd); netif_shutdown_all(); memcpy((void *)0xf0000, newaltboot, newaltboot_end - newaltboot); __syncicache((void *)0xf0000, newaltboot_end - newaltboot); printf("Restarting...\n"); run((void *)1, argv, (void *)0x100000, (void *)len, (void *)0xf0000); } err = fdloadfile(fd, marks, LOAD_KERNEL); close(fd); if (err < 0) continue; printf("entry=%p, ssym=%p, esym=%p\n", (void *)marks[MARK_ENTRY], (void *)marks[MARK_SYM], (void *)marks[MARK_END]); bootinfo = (void *)0x4000; bi_init(bootinfo); bi_add(&bi_cons, BTINFO_CONSOLE, sizeof(bi_cons)); bi_add(&bi_mem, BTINFO_MEMORY, sizeof(bi_mem)); bi_add(&bi_clk, BTINFO_CLOCK, sizeof(bi_clk)); bi_add(&bi_path, BTINFO_BOOTPATH, sizeof(bi_path)); bi_add(&bi_rdev, BTINFO_ROOTDEVICE, sizeof(bi_rdev)); bi_add(&bi_fam, BTINFO_PRODFAMILY, sizeof(bi_fam)); if (brdtype == BRD_SYNOLOGY || brdtype == BRD_DLINKDSM) { /* need to pass this MAC address to kernel */ bi_add(&bi_net, BTINFO_NET, sizeof(bi_net)); } if (modules_enabled) { if (fsmod != NULL) module_add(fsmod); kmodloadp = marks[MARK_END]; btinfo_modulelist = NULL; module_load(bname); if (btinfo_modulelist != NULL && btinfo_modulelist->num > 0) bi_add(btinfo_modulelist, BTINFO_MODULELIST, btinfo_modulelist_size); } launchfixup(); netif_shutdown_all(); __syncicache((void *)marks[MARK_ENTRY], (u_int)marks[MARK_SYM] - (u_int)marks[MARK_ENTRY]); run((void *)marks[MARK_SYM], (void *)marks[MARK_END], (void *)howto, bootinfo, (void *)marks[MARK_ENTRY]); /* should never come here */ printf("exec returned. Restarting...\n"); _rtt(); } loadfail: printf("load failed. Restarting...\n"); _rtt(); }
/* * This gets arguments from the first stage boot lader, calls PROM routines * to open and load the program to boot, and then transfers execution to * that new program. */ int main(int argc, char **argv) { char *name, **namep, *dev, *kernel; char bootname[PATH_MAX], bootpath[PATH_MAX]; int win; u_long marks[MARK_MAX]; struct btinfo_symtab bi_syms; struct btinfo_bootpath bi_bpath; extern void prom_init(void); void (*entry)(int, char **, char **, u_int, char *); prom_init(); /* print a banner */ printf("\n"); printf("NetBSD/mipsco " NETBSD_VERS " " BOOT_TYPE_NAME " Bootstrap, Revision %s\n", bootprog_rev); /* initialise bootinfo structure early */ bi_init(BOOTINFO_ADDR); dev = name = NULL; if (argc > 1) { kernel = devsplit(argv[1], bootname); if (*bootname) { dev = bootname; if (*kernel) name = argv[1]; ++argv; --argc; } } if (dev == NULL) { (void) devsplit(argv[0], bootname); dev = bootname; } memset(marks, 0, sizeof marks); if (name != NULL) win = (loadfile(name, marks, LOAD_KERNEL) == 0); else { win = 0; for (namep = kernelnames, win = 0; *namep != NULL && !win; namep++) { kernel = *namep; strcpy(bootpath, dev); strcat(bootpath, kernel); printf("Loading: %s\n", bootpath); win = (loadfile(bootpath, marks, LOAD_ALL) != -1); if (win) { name = bootpath; } } } if (!win) goto fail; strncpy(bi_bpath.bootpath, kernel, BTINFO_BOOTPATH_LEN); bi_add(&bi_bpath, BTINFO_BOOTPATH, sizeof(bi_bpath)); entry = (void *) marks[MARK_ENTRY]; bi_syms.nsym = marks[MARK_NSYM]; bi_syms.ssym = marks[MARK_SYM]; bi_syms.esym = marks[MARK_END]; bi_add(&bi_syms, BTINFO_SYMTAB, sizeof(bi_syms)); printf("Starting at 0x%x\n\n", (u_int)entry); (*entry)(argc, argv, NULL, BOOTINFO_MAGIC, (char *)BOOTINFO_ADDR); (void)printf("KERNEL RETURNED!\n"); fail: (void)printf("Boot failed! Halting...\n"); return (0); }
/** * @brief Does both division and modulo calculations. * * Used extensively when doing classical reduction. * @param ctx [in] The bigint session context. * @param u [in] A bigint which is the numerator. * @param v [in] Either the denominator or the modulus depending on the mode. * @param is_mod [n] Determines if this is a normal division (0) or a reduction * (1). * @return The result of the division/reduction. */ bigint * ICACHE_FLASH_ATTR bi_divide(BI_CTX *ctx, bigint *u, bigint *v, int is_mod) { int n = v->size, m = u->size-n; int j = 0, orig_u_size = u->size; uint8_t mod_offset = ctx->mod_offset; comp d; bigint *quotient, *tmp_u; comp q_dash; check(u); check(v); /* if doing reduction and we are < mod, then return mod */ if (is_mod && bi_compare(v, u) > 0) { bi_free(ctx, v); return u; } quotient = alloc(ctx, m+1); tmp_u = alloc(ctx, n+1); v = trim(v); /* make sure we have no leading 0's */ d = (comp)((long_comp)COMP_RADIX/(V1+1)); /* clear things to start with */ memset(quotient->comps, 0, ((quotient->size)*COMP_BYTE_SIZE)); /* normalise */ if (d > 1) { u = bi_int_multiply(ctx, u, d); if (is_mod) { v = ctx->bi_normalised_mod[mod_offset]; } else { v = bi_int_multiply(ctx, v, d); } } if (orig_u_size == u->size) /* new digit position u0 */ { more_comps(u, orig_u_size + 1); } do { /* get a temporary short version of u */ memcpy(tmp_u->comps, &u->comps[u->size-n-1-j], (n+1)*COMP_BYTE_SIZE); /* calculate q' */ if (U(0) == V1) { q_dash = COMP_RADIX-1; } else { q_dash = (comp)(((long_comp)U(0)*COMP_RADIX + U(1))/V1); if (v->size > 1 && V2) { /* we are implementing the following: if (V2*q_dash > (((U(0)*COMP_RADIX + U(1) - q_dash*V1)*COMP_RADIX) + U(2))) ... */ comp inner = (comp)((long_comp)COMP_RADIX*U(0) + U(1) - (long_comp)q_dash*V1); if ((long_comp)V2*q_dash > (long_comp)inner*COMP_RADIX + U(2)) { q_dash--; } } } /* multiply and subtract */ if (q_dash) { int is_negative; tmp_u = bi_subtract(ctx, tmp_u, bi_int_multiply(ctx, bi_copy(v), q_dash), &is_negative); more_comps(tmp_u, n+1); Q(j) = q_dash; /* add back */ if (is_negative) { Q(j)--; tmp_u = bi_add(ctx, tmp_u, bi_copy(v)); /* lop off the carry */ tmp_u->size--; v->size--; } } else { Q(j) = 0; } /* copy back to u */ memcpy(&u->comps[u->size-n-1-j], tmp_u->comps, (n+1)*COMP_BYTE_SIZE); } while (++j <= m); bi_free(ctx, tmp_u); bi_free(ctx, v); if (is_mod) /* get the remainder */ { bi_free(ctx, quotient); return bi_int_divide(ctx, trim(u), d); } else /* get the quotient */ { bi_free(ctx, u); return trim(quotient); } }
int main() { bi_initialize(); bigint sum = int_to_bi(0); int a,b,c,d,e,f,g,h,i,j; int n; for (a=9;a>0;--a) { for (b=9;b>=0;--b) { if (b==a) continue; for (c=9;c>=0;--c) { if (c==a||c==b) continue; for (d=9;d>=0;--d) { if (d==a||d==b||d==c) continue; for (e=9;e>=0;--e) { if (e==a||e==b||e==c||e==d) continue; for (f=9;f>=0;--f) { if (f==a||f==b||f==c||f==d||f==e) continue; for (g=9;g>=0;--g) { if (g==a||g==b||g==c||g==d||g==e||g==f) continue; for (h=9;h>=0;--h) { if (h==a||h==b||h==c||h==d||h==e||h==f||h==g) continue; for (i=9;i>=0;--i) { if (i==a||i==b||i==c||i==d||i==e||i==f||i==g||i==h) continue; for (j=9;j>=0;--j) { if (j==a||j==b||j==c||j==d||j==e||j==f||j==g||j==h||j==i) continue; if ((b * 100 + c * 10 + d) % 2 == 0) if ((c * 100 + d * 10 + e) % 3 == 0) if ((d * 100 + e * 10 + f) % 5 == 0) if ((e * 100 + f * 10 + g) % 7 == 0) if ((f * 100 + g * 10 + h) % 11 == 0) if ((g * 100 + h * 10 + i) % 13 == 0) if ((h * 100 + i * 10 + j) % 17 == 0) { n = b * 100000000 + c * 10000000 + d * 1000000 + e * 100000 + f * 10000 + g * 1000 + h * 100 + i * 10 + j * 1; sum = bi_add(sum, int_to_bi(n)); sum = bi_add(sum, bi_int_multiply(int_to_bi(1000000000), a)); #ifdef DEBUG printf("%d%d%d%d%d%d%d%d%d%d\n", a,b,c,d,e,f,g,h,i,j); bi_print(stdout,bi_copy(sum)); printf("\n"); #endif } } } } } } } } } } } bi_print(stdout,sum); printf("\n"); bi_terminate(); return 0; }
/* * Entry point. * Parse PROM boot string, load the kernel and jump into it */ int main(unsigned int memsize) { char **namep, *dev, *kernel, *bi_addr; char bootpath[PATH_MAX]; int win; u_long marks[MARK_MAX]; void (*entry)(unsigned int, u_int, char *); struct btinfo_flags bi_flags; struct btinfo_symtab bi_syms; struct btinfo_bootpath bi_bpath; struct btinfo_howto bi_howto; int addr, speed, howto; try_bootp = 1; /* Initialize boot info early */ dev = NULL; kernel = NULL; howto = 0x0; bi_flags.bi_flags = 0x0; bi_addr = bi_init(); lcd_init(); cobalt_id = read_board_id(); prominit(memsize); if (cninit(&addr, &speed) != NULL) bi_flags.bi_flags |= BI_SERIAL_CONSOLE; print_banner(memsize); memset(marks, 0, sizeof marks); get_bsdbootname(&dev, &kernel, &howto); if (kernel != NULL) { DPRINTF(("kernel: %s\n", kernel)); kernelnames[0] = kernel; kernelnames[1] = NULL; } else { DPRINTF(("kernel: NULL\n")); } win = 0; DPRINTF(("Kernel names: %p\n", kernelnames)); for (namep = kernelnames, win = 0; (*namep != NULL) && !win; namep++) { kernel = *namep; bootpath[0] = '\0'; strcpy(bootpath, dev ? dev : DEFBOOTDEV); strcat(bootpath, ":"); strcat(bootpath, kernel); lcd_loadfile(bootpath); printf("Loading: %s", bootpath); if (howto) printf(" (howto 0x%x)", howto); printf("\n"); patch_bootstring(bootpath); win = (loadfile(bootpath, marks, LOAD_ALL) != -1); } if (win) { strncpy(bi_bpath.bootpath, kernel, BTINFO_BOOTPATH_LEN); bi_add(&bi_bpath, BTINFO_BOOTPATH, sizeof(bi_bpath)); entry = (void *)marks[MARK_ENTRY]; bi_syms.nsym = marks[MARK_NSYM]; bi_syms.ssym = marks[MARK_SYM]; bi_syms.esym = marks[MARK_END]; bi_add(&bi_syms, BTINFO_SYMTAB, sizeof(bi_syms)); bi_add(&bi_flags, BTINFO_FLAGS, sizeof(bi_flags)); bi_howto.bi_howto = howto; bi_add(&bi_howto, BTINFO_HOWTO, sizeof(bi_howto)); entry = (void *)marks[MARK_ENTRY]; DPRINTF(("Bootinfo @ 0x%lx\n", (u_long)bi_addr)); printf("Starting at 0x%lx\n\n", (u_long)entry); (*entry)(memsize, BOOTINFO_MAGIC, bi_addr); } delay(20000); lcd_failed(); (void)printf("Boot failed! Rebooting...\n"); return 0; }
void boot(uint32_t a0, uint32_t a1, uint32_t a2, uint32_t a3, uint32_t a4, uint32_t a5) { int fd, i; char *netbsd = ""; int maxmem; u_long marks[MARK_MAX]; char devname[32], file[32]; void (*entry)(uint32_t, uint32_t, uint32_t, uint32_t, uint32_t, uint32_t); struct btinfo_symtab bi_sym; struct btinfo_bootarg bi_arg; struct btinfo_bootpath bi_bpath; struct btinfo_systype bi_sys; int loadflag; /* Clear BSS. */ memset(_edata, 0, _end - _edata); /* * XXX a3 contains: * maxmem (nws-3xxx) * argv (apbus-based machine) */ if (a3 >= 0x80000000) apbus = 1; else apbus = 0; if (apbus) _sip = (void *)a4; printf("%s Secondary Boot, Revision %s\n", bootprog_name, bootprog_rev); if (apbus) { char *bootdev = (char *)a1; int argc = a2; char **argv = (char **)a3; DPRINTF("APbus-based system\n"); DPRINTF("argc = %d\n", argc); for (i = 0; i < argc; i++) { DPRINTF("argv[%d] = %s\n", i, argv[i]); if (argv[i][0] != '-' && *netbsd == 0) netbsd = argv[i]; } maxmem = _sip->apbsi_memsize; maxmem -= 0x100000; /* reserve 1MB for ROM monitor */ DPRINTF("howto = 0x%x\n", a0); DPRINTF("bootdev = %s\n", (char *)a1); DPRINTF("bootname = %s\n", netbsd); DPRINTF("maxmem = 0x%x\n", maxmem); /* XXX use "sonic()" instead of "tftp()" */ if (strncmp(bootdev, "tftp", 4) == 0) bootdev = "sonic"; strcpy(devname, bootdev); if (strchr(devname, '(') == NULL) strcat(devname, "()"); } else { int bootdev = a1; char *bootname = (char *)a2; int ctlr, unit, part, type; DPRINTF("HB system.\n"); /* bootname is "/boot" by default on HB system. */ if (bootname && strcmp(bootname, "/boot") != 0) netbsd = bootname; maxmem = a3; DPRINTF("howto = 0x%x\n", a0); DPRINTF("bootdev = 0x%x\n", a1); DPRINTF("bootname = %s\n", netbsd); DPRINTF("maxmem = 0x%x\n", maxmem); ctlr = BOOTDEV_CTLR(bootdev); unit = BOOTDEV_UNIT(bootdev); part = BOOTDEV_PART(bootdev); type = BOOTDEV_TYPE(bootdev); if (devs[type] == NULL) { printf("unknown bootdev (0x%x)\n", bootdev); _rtt(); } snprintf(devname, sizeof(devname), "%s(%d,%d,%d)", devs[type], ctlr, unit, part); } printf("Booting %s%s\n", devname, netbsd); /* use user specified kernel name if exists */ if (*netbsd) { kernels[0] = netbsd; kernels[1] = NULL; } loadflag = LOAD_KERNEL; if (devname[0] == 'f') /* XXX */ loadflag &= ~LOAD_BACKWARDS; marks[MARK_START] = 0; for (i = 0; kernels[i]; i++) { snprintf(file, sizeof(file), "%s%s", devname, kernels[i]); DPRINTF("trying %s...\n", file); fd = loadfile(file, marks, loadflag); if (fd != -1) break; } if (kernels[i] == NULL) _rtt(); DPRINTF("entry = 0x%x\n", (int)marks[MARK_ENTRY]); DPRINTF("ssym = 0x%x\n", (int)marks[MARK_SYM]); DPRINTF("esym = 0x%x\n", (int)marks[MARK_END]); bi_init(BOOTINFO_ADDR); bi_sym.nsym = marks[MARK_NSYM]; bi_sym.ssym = marks[MARK_SYM]; bi_sym.esym = marks[MARK_END]; bi_add(&bi_sym, BTINFO_SYMTAB, sizeof(bi_sym)); bi_arg.howto = a0; bi_arg.bootdev = a1; bi_arg.maxmem = maxmem; bi_arg.sip = (int)_sip; bi_add(&bi_arg, BTINFO_BOOTARG, sizeof(bi_arg)); strcpy(bi_bpath.bootpath, file); bi_add(&bi_bpath, BTINFO_BOOTPATH, sizeof(bi_bpath)); bi_sys.type = apbus ? NEWS5000 : NEWS3400; /* XXX */ bi_add(&bi_sys, BTINFO_SYSTYPE, sizeof(bi_sys)); entry = (void *)marks[MARK_ENTRY]; if (apbus) apcall_flushcache(); else mips1_flushicache(entry, marks[MARK_SYM] - marks[MARK_ENTRY]); printf("\n"); (*entry)(a0, a1, a2, a3, a4, a5); }
int main(void) { int error, i; char kernel[MAX_PROM_PATH]; const char *k; u_long marks[MARK_MAX], bootinfo; struct btinfo_symtab bi_sym; struct btinfo_boothowto bi_howto; void *arg; #ifdef HEAP_VARIABLE { extern char end[]; setheap((void *)ALIGN(end), (void *)0xffffffff); } #endif prom_init(); mmu_init(); printf(">> %s, Revision %s\n", bootprog_name, bootprog_rev); /* massage machine prom */ prom_patch(); /* * get default kernel. */ k = prom_getbootfile(); if (k != NULL && *k != '\0') { i = -1; /* not using the kernels */ strcpy(kernel, k); } else { i = 0; strcpy(kernel, kernels[i]); } k = prom_getbootpath(); if (k && *k) strcpy(prom_bootdevice, k); boothowto = bootoptions(prom_getbootargs()); for (;;) { /* * ask for a kernel first .. */ if (boothowto & RB_ASKNAME) { printf("device[%s] (\"halt\" to halt): ", prom_bootdevice); gets(dbuf); if (strcmp(dbuf, "halt") == 0) _rtt(); if (dbuf[0]) strcpy(prom_bootdevice, dbuf); printf("boot (press RETURN to try default list): "); gets(fbuf); if (fbuf[0]) strcpy(kernel, fbuf); else { boothowto &= ~RB_ASKNAME; i = 0; strcpy(kernel, kernels[i]); } } printf("Booting %s\n", kernel); if ((error = loadk(kernel, marks)) == 0) break; if (error != ENOENT) { printf("Cannot load %s: error=%d\n", kernel, error); boothowto |= RB_ASKNAME; } /* * if we have are not in askname mode, and we aren't using the * prom bootfile, try the next one (if it exits). otherwise, * go into askname mode. */ if ((boothowto & RB_ASKNAME) == 0 && i != -1 && kernels[++i]) { strcpy(kernel, kernels[i]); printf(": trying %s...\n", kernel); } else { printf("\n"); boothowto |= RB_ASKNAME; } } marks[MARK_END] = (((u_long)marks[MARK_END] + sizeof(u_long) - 1)) & (-sizeof(u_long)); arg = (prom_version() == PROM_OLDMON) ? (void *)PROM_LOADADDR : romp; /* Setup boot info structure at the end of the kernel image */ bootinfo = bi_init(marks[MARK_END] & loadaddrmask); /* Add kernel symbols to bootinfo */ bi_sym.nsym = marks[MARK_NSYM] & loadaddrmask; bi_sym.ssym = marks[MARK_SYM] & loadaddrmask; bi_sym.esym = marks[MARK_END] & loadaddrmask; bi_add(&bi_sym, BTINFO_SYMTAB, sizeof(bi_sym)); /* Add boothowto */ bi_howto.boothowto = boothowto; bi_add(&bi_howto, BTINFO_BOOTHOWTO, sizeof(bi_howto)); /* Add kernel path to bootinfo */ i = sizeof(struct btinfo_common) + strlen(kernel) + 1; /* Impose limit (somewhat arbitrary) */ if (i < BOOTINFO_SIZE / 2) { union { struct btinfo_kernelfile bi_file; char x[i]; } U; strcpy(U.bi_file.name, kernel); bi_add(&U.bi_file, BTINFO_KERNELFILE, i); } (*(entry_t)marks[MARK_ENTRY])(arg, 0, 0, 0, bootinfo, DDB_MAGIC2); _rtt(); }
int main(int argc, char **argv) { const char *kernel = NULL; const char *bootpath = NULL; char bootfile[PATH_MAX]; void (*entry)(int, char *[], u_int, void *); u_long marks[MARK_MAX]; int win = 0; int i; int ch; /* print a banner */ printf("\n"); printf("%s Bootstrap, Revision %s\n", bootprog_name, bootprog_rev); memset(marks, 0, sizeof marks); /* initialise bootinfo structure early */ bi_init(bootinfo); #ifdef BOOT_DEBUG for (i = 0; i < argc; i++) printf("argv[%d] = %s\n", i, argv[i]); #endif /* Parse arguments, if present. */ while ((ch = getopt(argc, argv, "v")) != -1) { switch (ch) { case 'v': debug = 1; break; } } environment = &argv[1]; bootpath = firmware_getenv("OSLoadPartition"); if (bootpath == NULL) bootpath = arcbios_GetEnvironmentVariable("OSLoadPartition"); if (bootpath == NULL) { /* XXX need to actually do the fixup */ printf("OSLoadPartition is not specified.\n"); return 0; } DPRINTF("bootpath = %s\n", bootpath); /* * Grab OSLoadFilename from ARCS. */ kernel = firmware_getenv("OSLoadFilename"); if (kernel == NULL) kernel = arcbios_GetEnvironmentVariable("OSLoadFilename"); DPRINTF("kernel = %s\n", kernel ? kernel : "<null>"); /* * The first arg is assumed to contain the name of the kernel to boot, * if it a) does not start with a hyphen and b) does not contain * an equals sign. */ for (i = 1; i < argc; i++) { if (((strchr(argv[i], '=')) == NULL) && (argv[i][0] != '-')) { kernel = argv[i]; break; } } if (kernel != NULL) { /* * if the name contains parenthesis, we assume that it * contains the bootpath and ignore anything passed through * the environment */ if (strchr(kernel, '(')) win = loadfile(kernel, marks, LOAD_KERNEL); else { strcpy(bootfile, bootpath); strcat(bootfile, kernel); win = loadfile(bootfile, marks, LOAD_KERNEL); } } else { i = 1; while (kernelnames[i] != NULL) { strcpy(bootfile, bootpath); strcat(bootfile, kernelnames[i]); kernel = kernelnames[i]; win = loadfile(bootfile, marks, LOAD_KERNEL); if (win != -1) break; i++; } } if (win < 0) { printf("Boot failed! Halting...\n"); (void)getchar(); return 0; } strlcpy(bi_bpath.bootpath, kernel, BTINFO_BOOTPATH_LEN); bi_add(&bi_bpath, BTINFO_BOOTPATH, sizeof(bi_bpath)); bi_syms.nsym = marks[MARK_NSYM]; bi_syms.ssym = marks[MARK_SYM]; bi_syms.esym = marks[MARK_END]; bi_add(&bi_syms, BTINFO_SYMTAB, sizeof(bi_syms)); entry = (void *)marks[MARK_ENTRY]; if (debug) { printf("Starting at %p\n\n", entry); printf("nsym 0x%lx ssym 0x%lx esym 0x%lx\n", marks[MARK_NSYM], marks[MARK_SYM], marks[MARK_END]); } (*entry)(argc, argv, BOOTINFO_MAGIC, bootinfo); printf("Kernel returned! Halting...\n"); return 0; }