Ejemplo n.º 1
0
int main(int argc, char *argv[]) {
    int num = FACT;
    if (argc == 2) {
        num = atoi(argv[1]);
    }

    //actual problem 16 code
    big_int ans = bi_init("1");
    big_int* inc = malloc(sizeof(big_int));
    *inc = bi_init("1");
    int i;
    for(i = 1; i <= num; i++) {
        bi_set_int(inc, i);
        ans =  bi_mult(ans, *inc);
    }
    printf("%d! = %s\n", num, bi_get(ans));
    int sum = 0;
    for(i = 1; i <= DIGITS; i++) {
        if (ans.number[i] == EON) {
            break;
        }
        sum += ans.number[i];
    }
    printf("Sum of digits: %d\n", sum);
}
Ejemplo n.º 2
0
int test_mult_increase() {
    big_int a= bi_init("64");
    big_int b= bi_init("2");
    if (0 == strcmp(bi_get(bi_mult(a,b)), "128")) {
        return 1;
    }
    printf("\nUnexpected Value: %s\n", bi_get(bi_mult(a,b)));
    return 0;
}
Ejemplo n.º 3
0
int test_mult_double() {
    big_int a= bi_init("105");
    big_int b= bi_init("5");
    if (0 == strcmp(bi_get(bi_mult(a,b)), "525")) {
        return 1;
    }
    printf("\nUnexpected Value: %s\n", bi_get(bi_mult(a,b)));
    return 0;
}
Ejemplo n.º 4
0
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;
}
Ejemplo n.º 5
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;
}
Ejemplo n.º 6
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;
}
Ejemplo n.º 7
0
int test_get_from_string() {
    big_int a = bi_init("500");
    if (0 != strcmp(bi_get(a), "500")) {
        printf("\nUnexpected Value: *%s*\n", bi_get(a));
        return 0;
    }
    return 1;
}
Ejemplo n.º 8
0
//unit tests - note to self - find a good unit test library
//and never do this again
int test_set_from_string() {
    big_int a = bi_init("500");
    if (a.number[3] == EON && a.number[2] ==5 && a.number[1] == 0 && a.number[0] == 0) {
        return 1;
    }
    printf("\nUnexpected Values: %d, %d, %d, %d\n", a.number[3], a.number[2], a.number[1], a.number[0]);
    return 0;
}
Ejemplo n.º 9
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");
}
Ejemplo n.º 10
0
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;
}
Ejemplo n.º 11
0
int main()
{
    BIGNUM   *x, *y, *exp, *m, *order, *cof;
    BIGNUM   t, store[30];
    COMPLEX  *a, *b, *r;
    EC_POINT *point, *Q;
    int      i;

    x = BN_new();
    y = BN_new();
    order = BN_new();
    exp = BN_new();
    m = BN_new();

    a = COMP_new();
    b = COMP_new();
    r = COMP_new();
    for( i = 0; i < 30; i++ )
    	BN_init( &(store[i]) );

    if ( Context == NULL )
    	Context = BN_CTX_new();

    bi_init( &malloc );

    group = EC_GROUP_new( EC_GFp_simple_method() );
    if ( group == NULL )
    	goto err;

    if(!BN_set_word(m, 43l))
    	goto err;
    BN_set_word(x, 1l);
    BN_set_word(y, 0l);

    if ( !EC_GROUP_set_curve_GFp( group, m, x, y, Context) )
    	goto err;

    BN_set_word(x, 23l);
    BN_set_word(y, 8l);
    BN_set_word(order, 11l);

    point = EC_POINT_new( group );
    EC_POINT_set_affine_coordinates_GFp( group, point, x, y, Context );

    cof = BN_new();
    BN_set_word( cof, 4 );
    EC_GROUP_set_generator( group, point, order, cof );

    if ( EC_GROUP_check( group, Context ) )
    	printf(" group set is ok \n");

    TSS_DAA_ISSUER_KEY   issuer_key;
    TSS_DAA_ISSUER_PROOF issuer_proof;
    TSS_DAA_JOIN_issuer_setup(&issuer_key, &issuer_proof);


//    printf("\n");
//    BN_set_word(x, 41l);
//    BN_mod_inverse(x, x, m, Context);
//    BN_print_fp(stdout, x);
//
//    printf("\n");
//    BN_set_word(x, 11l);
//    BN_mod_inverse(x, x, m, Context);
//    BN_print_fp(stdout, x);

    char *str = "abcdefghijklmnop";
    Q = map_to_point( str );

    BN_set_word(x, 23l);
    BN_set_word(y, 8l);
    BN_set_word(order, 11l);

    Q = EC_POINT_new( group );
    EC_POINT_set_affine_coordinates_GFp( group, Q, x, y, Context );

    Tate( point, Q, order, 0,  store, a );
    printf("tate pair  t(p, Q) =:\n a.x: ");
    BN_print_fp(stdout, &a->x);
    printf("\na.y: ");
    BN_print_fp(stdout, &a->y);

    EC_POINT_dbl( group, point, point, Context);
    EC_POINT_get_affine_coordinates_GFp( group, point, x, y, Context);
    printf("2A.x =:\n");
    BN_print_fp(stdout, x);
    printf("2P.y= :\n");
    BN_print_fp(stdout, y);

    Tate( point, Q, order, 0,  store, a );
    printf("tate pair  t(2p, Q) =:\n a.x: ");
    BN_print_fp(stdout, &a->x);
    printf("\na.y: ");
    BN_print_fp(stdout, &a->y);

    BN_free( x );
    BN_free( y );
    BN_free( exp );
    BN_free( m );
    BN_free( order );
    BN_free( cof );

    COMP_free( a );
    COMP_free( b );
    COMP_free( r );

	return 0;
err:
	BN_free( &t );
	BN_free( x );
	BN_free( y );
	BN_free( exp );
	BN_free( m );
    BN_free( order );
    BN_free( cof );

	COMP_free( a );
	COMP_free( b );
	COMP_free( r );

	return 0;
}
Ejemplo n.º 12
0
int main(int argc, char *argv[]) {
	TSS_HCONTEXT hContext;
	TSS_RESULT result;
	TSS_HTPM hTPM;
	TSS_HPOLICY hPolicy;
	char *credential_filename = DEFAULT_CREDENTIAL_FILENAME;
	UINT32 nonceVerifierLength;
	BYTE *nonceVerifier;
	TSS_HDAA hDAA;
	TSS_DAA_CREDENTIAL *hDaaCredential;
	TSS_DAA_SIGN_DATA signData;
	TSS_DAA_SIGNATURE daaSignature;
	TSS_DAA_SELECTED_ATTRIB revealAttributes;
	char *szTpmPasswd = DEFAULT_OWN_PASSWD;
	char *message = NULL;
	BYTE **attributes = NULL;
	FILE *file;
	char *param;
	int i, length, rv;
	bi_ptr random = NULL;
	TSS_BOOL isCorrect;
	EVP_MD_CTX *mdctx;
	TSS_HKEY hKEY;

	init_tss_version( &signData);
	init_tss_version( &daaSignature);
	init_tss_version( &revealAttributes);
	i = 1;
	while( i < argc) {
		param = argv[ i];
		if ( strcmp( param, "-m") == 0 || strcmp( param, "--message") == 0) {
			i++;
			if( i == argc) return print_usage( argv[0]);
			message = argv[i];
		} else if( strcmp( param, "-cr") == 0 || strcmp( param, "--credential") == 0){
			i++;
			if( i == argc) return print_usage( argv[0]);
			credential_filename = argv[i];
		} else if( strcmp( param, "-pw") == 0 || strcmp( param, "--passwd") == 0){
			i++;
			if( i == argc) return print_usage( argv[0]);
			szTpmPasswd = argv[i];
		} else {
			fprintf(stderr, "%s:unrecognized option `%s'\n", argv[0], param);
			return print_usage( argv[0]);
		}
		i++;
	}
	bi_init( NULL);
	printf("Loading credential: %s ", credential_filename);
	file = fopen( credential_filename, "r");
	if( (hDaaCredential = load_TSS_DAA_CREDENTIAL( file)) == 0) {
		LogError( "[test_join]: Error when loading \'%s\': %s\n",
			credential_filename,
			strerror( errno));
		result = TSS_E_FAIL;
		goto out_close;
	}
	fclose( file);
	printf("Done\n");

	// Create Context
	LogDebug("Create Context");
	result = Tspi_Context_Create( &hContext );
	if ( result != TSS_SUCCESS )
	{
		LogError( "Tspi_Context_Create %d\n", result );
		goto out;
	}
	// Connect to Context
	result = Tspi_Context_Connect( hContext, NULL );
	if ( result != TSS_SUCCESS) goto out_close;
	printf("\nConnect to the context: %X\n", hContext);

	if( (result = Tspi_Context_GetTpmObject( hContext, &hTPM)) != TSS_SUCCESS)
		goto out_close;
	// Get the correct policy using the TPM ownership PASSWD
	if( (result = Tspi_GetPolicyObject( hTPM, TSS_POLICY_USAGE, &hPolicy)) != TSS_SUCCESS)
		goto out_close;
	if( (result = Tspi_Policy_SetSecret( hPolicy,
						TSS_SECRET_MODE_PLAIN,
						strlen( szTpmPasswd),
						szTpmPasswd)) != TSS_SUCCESS)
		goto out_close;
	LogDebug("Tspi_Policy_SetSecret hPolicy received;%d", hPolicy);

	//Create Object
	result = obj_daa_add( hContext, &hDAA);
	if (result != TSS_SUCCESS) {
		LogError("Tspi_Context_CreateObject:%d", result);
		Tspi_Context_Close(hContext);
		LogError("%s: %s", argv[0], err_string(result));
		exit(result);
	}
	LogDebug("created DAA object:%X", hDAA);

	// TODO: verifier base name ??
	result = Tspi_DAA_VerifyInit(
		hDAA,	// in
		&nonceVerifierLength,	// out
		&nonceVerifier,	// out
		0, //baseNameLength,	// out
		NULL //baseName		// out
	);
	if (result != TSS_SUCCESS) goto out_close;
	LogDebug("Verify Init return nonceVerifier [%s]",
			dump_byte_array( nonceVerifierLength, nonceVerifier));

	create_TSS_DAA_SELECTED_ATTRIB( &revealAttributes, 5, 0, 1, 1, 0, 0);

	mdctx = EVP_MD_CTX_create();

	// create the TSS_DAA_SIGN_DATA struct
	// .selector: 0 -> payload contains a handle to an AIK
	//            1 -> payload contains a hashed message
	if( message != NULL) {
		signData.selector = TSS_FLAG_DAA_SIGN_MESSAGE_HASH;
		signData.payloadFlag = TSS_FLAG_DAA_SIGN_MESSAGE_HASH;
		EVP_DigestInit(mdctx, DAA_PARAM_get_message_digest());
		EVP_DigestUpdate(mdctx,  (BYTE *)message, strlen( message));
		signData.payloadLength = EVP_MD_CTX_size(mdctx);
		signData.payload = (BYTE *)EVP_MD_CTX_create();
		EVP_DigestFinal(mdctx, signData.payload, NULL);
	} else {
		signData.selector = TSS_FLAG_DAA_SIGN_IDENTITY_KEY;
		result = Tspi_Context_CreateObject(
			hContext,	 //  in
			TSS_OBJECT_TYPE_RSAKEY,		//  in
			TSS_KEY_SIZE_2048,		//  in
			&hKEY	//  out
		);
		if( result != TSS_SUCCESS) goto out_close;

	}

	result = Tspi_TPM_DAA_Sign(
		hDAA,	// in
		hTPM,	// in
		(TSS_HKEY)hDaaCredential,	// in
		revealAttributes,	// in
		0, // verifierBaseNameLength,	// in
		NULL, // verifierBaseName,	// in
		nonceVerifierLength,	// in
		nonceVerifier,	// in
		signData,	// in
		&daaSignature	// out
	);
	if (result != TSS_SUCCESS) goto out_close;
	LogDebug("TPM_DAA_Sign return daaSignature [%s]",
			dump_byte_array( nonceVerifierLength, nonceVerifier));

	// generate attributes list but without copying the not revealed ones
	attributes = malloc( sizeof(BYTE *) * hDaaCredential->attributesLength);
	for( i=0; i < (int)(hDaaCredential->attributesLength); i++) {
		if( revealAttributes.indicesList[i]) {
			attributes[i] = (BYTE *)malloc( DAA_PARAM_SIZE_F_I / 8);
			memcpy( attributes[i],
				hDaaCredential->attributes[i],
				DAA_PARAM_SIZE_F_I / 8);
		} else {
			attributes[i] = NULL;
		}
	}

	result = Tspi_DAA_VerifySignature(
		hDAA,	// in
		daaSignature,	// in
		(TSS_HKEY)&(hDaaCredential->issuerPK),	// in
		signData,	// in
		hDaaCredential->attributesLength,	// in
		attributes,	// in
		nonceVerifierLength,	// in
		nonceVerifier,	// in
		0,	//baseNameLength,	//in
		NULL,	// in
		&isCorrect	// out
	);
	printf("Signature correct:%s\n", ( isCorrect ? "yes" : "no"));

out_close:
	EVP_MD_CTX_destroy(mdctx);
	if( attributes != NULL) {
		for( i=0; i<(int)hDaaCredential->attributesLength; i++) {
			if( attributes[i] != NULL) free( attributes[i]);
		}
		free( attributes);
	}
	if( random != NULL) bi_free_ptr( random);
	Tspi_Context_FreeMemory( hContext, NULL );
	Tspi_Context_Close( hContext );
out:
	bi_release();
	LogDebug("THE END result=%d:%s",result, err_string( result) );;
	return result;
}
Ejemplo n.º 13
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;
}
Ejemplo n.º 14
0
int main(int argc, char *argv[]) {
	char *filename = DEFAULT_FILENAME;
	int i=1;
	char *param;
	TSS_RESULT	result;
	TSS_HCONTEXT	hContext;
	TSS_HDAA hDAA;
	FILE *file;

//	foreground = 1; // for debug
	printf("Key Verification (%s:%s,%s)\n", argv[0], __DATE__, __TIME__);
	while( i < argc) {
		param = argv[ i];
		if( strcmp( param, "-if") == 0 || strcmp( param, "--issuer_file")) {
			i++;
			if( i == argc) return print_usage( argv[0]);
			filename = argv[i];
		} else {
			fprintf(stderr, "%s:unrecognized option `%s'\n", argv[0], param);
			return print_usage( argv[0]);
		}
		i++;
	}
	bi_init( NULL);
	printf("Loading issuer info (keypair & proof) -> 	\'%s\'", filename);
	file = fopen( filename, "r");
	if( file == NULL) {
		fprintf( stderr,
			"%s: Error when opening \'%s\': %s\n",
			argv[0],
			filename,
			strerror( errno));
		return -1;
	}
	KEY_PAIR_WITH_PROOF_internal *key_pair_with_proof = load_KEY_PAIR_WITH_PROOF( file);
	if( key_pair_with_proof == NULL) {
		fprintf( stderr,
			"%s: Error when reading \'%s\': %s\n",
			argv[0],
			filename,
			strerror( errno));
		return -1;
	}
	fclose( file);

	// Create Context
	printf("\nCreate Context\n");
	result = Tspi_Context_Create( &hContext );
	if ( result != TSS_SUCCESS )
	{
		fprintf( stderr, "Tspi_Context_Create %d\n", result );
		exit( result );
	}

	// Connect to Context
	printf("\nConnect to the context\n");
	result = Tspi_Context_Connect( hContext, NULL );
	if ( result != TSS_SUCCESS )
	{
		fprintf( stderr, "Tspi_Context_Connect error:%d\n", result );
		Tspi_Context_FreeMemory( hContext, NULL );
		Tspi_Context_Close( hContext );
		exit( result );
	}

	//TODO save key in the persistent store
	// result = ps_write_key( fd, )

	//Create Object
	result = obj_daa_add( hContext, &hDAA);
	if (result != TSS_SUCCESS) {
		LogError("Tspi_Context_CreateObject:%d", result);
        	Tspi_Context_Close(hContext);
		LogError("issuer_setup: %s", err_string(result));
		exit(result);
	}

	// TSS_HDAA, TSS_HKEY, TSS_DAA_PK_PROOF, TSS_BOOL*
	isCorrect( hDAA, key_pair_with_proof->pk, key_pair_with_proof->proof);
	obj_daa_remove( hDAA, hContext);
	printf("\nClosing the context\n");
	Tspi_Context_FreeMemory( hContext, NULL );
	Tspi_Context_Close( hContext );
	exit( 0 );
}
Ejemplo n.º 15
0
int main() {
    //unit tests
    printf("Test set_from_string ");
    if (test_set_from_string() == 1) {
        printf("pass\n");
    }
    else printf("fail\n");

    printf("Test get_from_string ");
    if (test_get_from_string() == 1) {
        printf("pass\n");
    }
    else printf("fail\n");

    printf("Test add_single ");
    if (test_add_single() == 1) {
        printf("pass\n");
    }
    else printf("fail\n");

    printf("Test add_double ");
    if (test_add_double() == 1) {
        printf("pass\n");
    }
    else printf("fail\n");

    printf("Test add_two_size ");
    if (test_add_two_size() == 1) {
        printf("pass\n");
    }
    else printf("fail\n");

    printf("Test mult_single ");
    if (test_mult_single() == 1) {
        printf("pass\n");
    }
    else printf("fail\n");
    
    printf("Test mult_double ");
    if (test_mult_double() == 1) {
        printf("pass\n");
    }
    else printf("fail\n");
    
    printf("Test mult_increase ");
    if (test_mult_increase() == 1) {
        printf("pass\n");
    }
    else printf("fail\n");

    //actual problem 16 code
    big_int a = bi_init("1");
    big_int two = bi_init("2");
    int i = 0;
    for(i = 0; i < DIGITS; i++) {
        a = bi_mult(a, two);
    }
    printf("2**1000: %s\n", bi_get(a));
    
    int sum = 0;
    for (i = 0; i < DIGITS; i++) {
        if (a.number[i] == EON) {
            break;
        }
        sum += a.number[i];
    }
    printf("Sum of digits: %d\n", sum);
}
Ejemplo n.º 16
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);
}
Ejemplo n.º 17
0
int main(int argc, char *argv[]) {
	int nb_platform_attr = DEFAULT_ISSUER_ATTRIBUTES;
	int nb_issuer_attr = DEFAULT_RECEIVER_ATTRIBUTES;
	char *filename = DEFAULT_FILENAME;
	char *issuer = DEFAULT_ISSUER;
	int i;
	char *param;
	TSS_HCONTEXT hContext;
	TSS_DAA_KEY_PAIR *key_pair;
	TSS_DAA_PK_PROOF *public_keyproof;
	TSS_RESULT result;
	TSS_HDAA hDAA;
	TSS_DAA_PK_PROOF_internal *public_keyproof_internal;
	TSS_DAA_PK_internal *pk;
	TSS_DAA_PRIVATE_KEY *private_key;
	DAA_PRIVATE_KEY_internal *private_key_internal;
	KEY_PAIR_WITH_PROOF_internal *key_pair_with_proof;

	printf("Issuer Setup (%s:%s,%s)\n", argv[0], __DATE__, __TIME__);
	i = 1;
	while( i < argc) {
		param = argv[ i];
		if         ( strcmp( param, "-if") == 0 || strcmp( param, "--issuer_file")) {
			i++;
			if( i == argc) return print_usage( argv[0]);
			filename = argv[i];
		} else if( strcmp( param, "-npa") == 0 || strcmp( param, "--nb_platform_attr")) {
			i++;
			if( i == argc) return print_usage( argv[0]);
			nb_platform_attr = atoi( argv[i]);
		} else if( strcmp( param, "-nia") == 0 || strcmp( param, "--nb_issuer_attr")) {
			i++;
			if( i == argc) return print_usage( argv[0]);
			nb_issuer_attr = atoi(argv[i]);
		} else if( strcmp( param, "-i") == 0 || strcmp( param, "--issuer")) {
			i++;
			if( i == argc) return print_usage( argv[0]);
			issuer = argv[i];
		} else {
			fprintf(stderr, 	"%s:unrecognized option `%s'\n", argv[0], param);
			return print_usage( argv[0]);
		}
		i++;
	}
	bi_init( NULL);
	// Create Context
	printf("Create Context\n");
	result = Tspi_Context_Create( &hContext );
	if ( result != TSS_SUCCESS )
	{
		fprintf( stderr, "Tspi_Context_Create %d\n", result );
		exit( result );
	}

	// Connect to Context
	printf("Connect to the context\n");
	result = Tspi_Context_Connect( hContext, NULL );
	if ( result != TSS_SUCCESS )
	{
		fprintf( stderr, "Tspi_Context_Connect error:%d\n", result );
		Tspi_Context_FreeMemory( hContext, NULL );
		Tspi_Context_Close( hContext );
		exit( result );
	}
	//Create Object
	result = obj_daa_add( hContext, &hDAA);
	if (result != TSS_SUCCESS) {
		goto close;
	}
	result = Tspi_DAA_IssueSetup(
		hDAA,	// in
		strlen( issuer),	// in
		(BYTE *)issuer,	// in
		nb_platform_attr,	// in
		nb_issuer_attr,	// in
		(TSS_HKEY *)&key_pair,	// out
		&public_keyproof);	// out
	if( result != TSS_SUCCESS) goto close;

	// TSS_DAA_KEY_PAIR_internal *key_pair_internal = DAA_KEY_PAIR_2_internal( key_pair);
	public_keyproof_internal = e_2_i_TSS_DAA_PK_PROOF( public_keyproof);
	pk = e_2_i_TSS_DAA_PK( key_pair->public_key);
	private_key = key_pair->private_key;
	private_key_internal = e_2_i_TSS_DAA_PRIVATE_KEY( private_key);
	key_pair_with_proof =
		(KEY_PAIR_WITH_PROOF_internal *)malloc( sizeof(KEY_PAIR_WITH_PROOF_internal));
	if( key_pair_with_proof == NULL) {
		fprintf("malloc of %d bytes failed", sizeof(KEY_PAIR_WITH_PROOF_internal));
		goto close;
	}
	key_pair_with_proof->pk = pk;
	key_pair_with_proof->private_key = private_key_internal;
	key_pair_with_proof->proof = public_keyproof_internal;

	printf("Saving key pair with proof  -> 	\'%s\'", filename);
	FILE *file = fopen( filename, "w");
	if( file == NULL) {
		fprintf( stderr, "%s: Error when saving \'%s\': %s\n",
			argv[0],
			filename,
			strerror( errno));
		return -1;
	}
	if( save_KEY_PAIR_WITH_PROOF( file, key_pair_with_proof) != 0) {
		fprintf( stderr, "%s: Error when saving \'%s\': %s\n",
			argv[0],
			filename,
			strerror( errno));
		return -1;
	}
	fclose( file);
	printf("\nDone.\n");
close:
	obj_daa_remove( hDAA, hContext);
	printf("Closing the context\n");
	Tspi_Context_FreeMemory( hContext, NULL );
	Tspi_Context_Close( hContext );
	bi_release();
	printf("Result: %d", result);
	return result;
}
Ejemplo n.º 18
0
int ZipOp::ZipStreamStream(void)
{
    fwindow_size = 0L;

    if (fGEncrypt)
    {
        fkey = fGPassword;

        if (!fkey || !*(fkey))
        {
            // use global
            if (GetUserPW() != DZ_ERR_GOOD)
                return -1;  // error

            fkey = fuser_key;
        }
    }

    fZipInfile = new ZStream(this, _T("0:<INSTREAM>"), fSS->fSSInput);

    AutoStream inz(&fZipInfile);
    fimax = fSS->Size;
    fcrc = crc32(0L, NULL, 0);
    fisize = 0;
    CB->SetArg1(1);
    CB->UserCB(zacCount);

    // Pass total filesize.
    CB->SetFileSize(fimax);
    CB->UserCB(zacSize);
    ulg f_crc = 0;
    __int64 fsz = 0;
    bool haveCRC = false;

    if (fkey)
    {
        if (!fNoPrecalc)
        {
            if (Verbose < 0)
                Notify(ITRACE,  _T("about to call Precalculate CRC"));

            // +++++ get CRC before we start
			CB->UserXItem(fimax, 13, _T("*PreCalculate"));
            __int64 pos1 = 0;

            if (!fZipInfile->IsFile)
                pos1 = fZipInfile->SetPosition(0, FILE_CURRENT); // get start posn

			f_crc = crc32(0L, NULL, 0);
            unsigned long byts;

            while (true)
            {
                unsigned ToRead = sizeof(fwindow);

                if (fimax > 0 && (fsz + ToRead) > fimax)
                {
                    ToRead = (unsigned)(fimax - fsz);

                    if (!ToRead)
                        break;
                }

                if (!fZipInfile->Read(fwindow, ToRead, &byts) || !byts)
                    break;

				fsz += byts;
				f_crc = crc32(f_crc, (const uch*)fwindow, (int)byts);
				CB->UserXProgress(byts, 13);

                if (Abort_Flag)
					Fatal(DZ_ERM_ABORT, 0);
            }

            fSS->CRC = f_crc;

            haveCRC = true;
            // reposition

            if (fZipInfile->SetPosition(pos1, FILE_BEGIN) != pos1)
            {
                if (Verbose)
					Notify(IVERBOSE, _T("Could not reposition %s [%s]"),
						fZipInfile->fname.c_str(), SysMsg().c_str());

				return  DZError(DZ_ERM_ERROR_SEEK);
			}

            if (fimax > fsz)
                fimax = fsz;
        }

        // ----- get CRC before we start
        // Since we do not yet know the crc here, we pretend that the crc is the
        //   modification time:
//    if (!haveCRC)
//      fSS->CRC = z->tim << 16;
        if (Verbose < 0)
            Notify(ITRACE,  _T("using supplied CRC %lu"), fSS->CRC);
    }

    // connect to output
    fZipOutfile = new ZStream(this, _T("0:<OUTSTREAM>"), fSS->fSSOutput);

    AutoStream outz(&fZipOutfile);

    CB->UserItem(fimax, _T("<INSTREAM>"));

	if (fkey)
		crypthead(fkey, fSS->CRC);  // write

    // Write stored or deflated file to zip file
    fSS->Method &= 0xFF;

    if (fSS->Method != DEFLATE)
        fSS->Method = 0;

    if (flevel < 1)
        fSS->Method = 0;

	int mthd = (int)fSS->Method;

    if (mthd == DEFLATE)
    {
        if (Verbose < 0)
            Notify(ITRACE,  _T("about to call Deflate"));

        bi_init();
        ush att = BINARY;
        ush flg = FLAG_ENCRYPT_BIT;

        // will be changed in deflate()
        ct_init(&att, &mthd);
        lm_init(flevel, &flg);

        // PERFORM THE DEFLATE
        fSS->Size = deflate();

        if (Abort_Flag)
			Fatal(DZ_ERM_ABORT, 0);
    }
    else
    {
        int k;

        if (Verbose)
            Notify(IVERBOSE, _T("Storing %s "), fZipInfile->fname.c_str());

        while ((k = read_buf(fwindow, sizeof(fwindow))) > 0
                && k != EOF)
        {
            if (Abort_Flag)
				Fatal(DZ_ERM_ABORT, 0);

			if (!zfwrite(fwindow,  (extent)k))
				return DZ_ERM_TEMP_FAILED;
        }

    }
    /*  Finished Item */
    CB->UserItem(-1, _T("<INSTREAM>")); // mark end of item
    CB->UserCB(zacEndOfBatch); // done with stream compression

    if (haveCRC)
    {
        if (f_crc != fcrc)
            Notify(DZ_ERR_ERROR_READ | IWARNING, _T(" File CRC changed while zipping: %s"),
                   fZipInfile->fname.c_str());

        if (fisize != fsz)
            Notify(DZ_ERR_ERROR_READ | IWARNING, _T(" File size changed while zipping: %s"),
                   fZipInfile->fname.c_str());
    }

    fSS->Size = fisize;

    fSS->CRC = fcrc;
    fSS->Method = (DWORD)mthd | (fkey ? 0xff00 : 0);
    return DZ_ERR_GOOD;
}
Ejemplo n.º 19
0
/*
 * 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);
}
Ejemplo n.º 20
0
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();
}
Ejemplo n.º 21
0
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();
}