tools/header_generation/create_hdr_common.c
/***************************************************************************
 * Function	:	create_hdr
 * Arguments	:	argc - Argument Count
 *			argv - Argumnet List
 * Return	:	SUCCESS or FAILURE
 * Description	:	Main function where execution starts
 ***************************************************************************/
Function     :       create_hdr
Arguments    :       argc - Argument Count
argv - Argumnet List
Return       :       SUCCESS or FAILURE
Description  :       Main function where execution starts
Function : create_hdr Arguments : argc - Argument Count argv - Argumnet List Return : SUCCESS or FAILURE Description : Main function where execution starts
int create_hdr(int argc, char **argv)
{
	enum cfg_taal cfg_taal;
	int ret, i, c;
	int option_index;
	uint32_t *srk;
	char ap_file[MAX_FNAME_LEN];
	uint32_t offset = 0;
	bool append_flag = false;
	printf("\n\t#----------------------------------------------------#");
	printf("\n\t#-------         --------     --------        -------#");
	printf("\n\t#------- CST (Code Signing Tool) Version 2.0  -------#");
	printf("\n\t#-------         --------     --------        -------#");
	printf("\n\t#----------------------------------------------------#");
	printf("\n");
	c = 0;
	option_index = 0;
	while (c != -1) {
		c = getopt_long(argc, argv, "", long_options, &option_index);
		if (c == -1)
			break;
		switch (c) {
			case 'h':
				printf("file name is %s\n", optarg);
				strcpy(gd.hdr_file_name, optarg);
				gd.hdr_file_flag = 1;
				break;
			case 'i':
				printf("file name is %s\n", optarg);
				strcpy(gd.entries[0].name, optarg);
				gd.entry1_flag = 1;
				break;
			case 'a':
				printf("file name is %s\n", optarg);
				strcpy(ap_file, optarg);
				append_flag = true;
				break;
			case 'o':
				offset = STR_TO_UL(optarg, 16);
				printf("offset is %d\n", offset);
				append_flag = true;
				break;
			default:
				 printf("?? getopt returned character code 0%o ??\n", c);
		}
	}
	if (gd.help_flag == 1) {
		print_usage(argv[0]);
		return SUCCESS;
	}
	if (append_flag == true) {
		if (strlen(ap_file) == 0 || offset == 0) {
			printf("Append options --append <filename> --append_offset <offset> both are required \n");
			return FAILURE;
		}
		strcpy(gd.ap_file[gd.ap_count].name, ap_file);
		gd.ap_file[gd.ap_count].offset = offset;
		gd.ap_count++;
	}
	if (optind != argc - 1) {
		printf("\nError!! Input File is not Specified");
		print_usage(argv[0]);
		return FAILURE;
	}
	crypto_print_attribution();
	gd.input_file = argv[optind];
	printf("\nInput File is %s\n", gd.input_file);
	cfg_taal = get_ta_from_file(gd.input_file);
	if (cfg_taal == TA_UNKNOWN_MAX) {
		printf("\n Unknown/Missing Platform name in Input file\n");
		return FAILURE;
	}
	ret = taal_parse_input_file(cfg_taal);
	if (ret != SUCCESS)
		return ret;
	ret = taal_calc_srk_hash(cfg_taal);
	if (ret != SUCCESS)
		return ret;
	if (gd.option_srk_hash == 0) {
		ret = taal_fill_structures(cfg_taal);
		if (ret != SUCCESS)
		return ret;
		ret = taal_calc_img_hash(cfg_taal);
		if (ret != SUCCESS)
			return ret;
		ret = taal_create_hdr(cfg_taal);
		if (ret != SUCCESS)
			return ret;
		if (gd.verbose_flag) {
			ret = taal_dump_header(cfg_taal);
			if (ret != SUCCESS)
				return ret;
			printf("\nImage Hash:\n");
			for (i = 0; i < SHA256_DIGEST_LENGTH; i++)
				printf("%02x", gd.img_hash[i]);
			if (gd.ie_table_size != 0) {
					printf(
					"\nIE Table Absolute Address: %x\n",
					gd.entries[0].addr_low);
			}
		}
		printf("\n\n************************************************");
		if (gd.option_img_hash == 1) {
			ret = create_img_hash_file();
			if (ret != SUCCESS)
				return ret;
			printf("\n* Image Hash Stored in File: %s",
				gd.img_hash_file_name);
			printf("\n* Header File is w/o Signature appended");
		} else {
			ret = calculate_signature();
			if (ret != SUCCESS)
				return ret;
			ret = append_signature();
			if (ret != SUCCESS)
				return ret;
			printf("\n* Header File is with Signature appended");
		}
		printf("\n************************************************\n");
		printf("\nHeader File Created: %s", gd.hdr_file_name);
	}
	if (gd.srk_hash_flag == 1) {
		printf("\n\nSRK (Public Key) Hash:\n");
		for (i = 0; i < SHA256_DIGEST_LENGTH; i++)
			printf("%02x", gd.srk_hash[i]);
		srk = (uint32_t *)gd.srk_hash;
		for (i = 0; i < SHA256_DIGEST_LENGTH / sizeof(uint32_t); i++)
			printf("\n\t SFP SRKHR%i = %08x", i, htonl(srk[i]));
	} else {
		printf("\nSRK (Public Key) Hash Not Available");
	}
	ret = get_apnd_img(gd.input_file);
	if (ret != SUCCESS)
		return ret;
	ret = add_apnd_img();
	if (ret != SUCCESS)
		return ret;
	printf("\n\n");
	return SUCCESS;
}
int create_hdr ( int argc , char * * argv ) { enum cfg_taal cfg_taal ; int ret , i , c ; int option_index ; uint32_t * srk ; char ap_file [ MAX_FNAME_LEN ] ; uint32_t offset = 0 ; bool append_flag = false ; printf ( " \n \t " ) ; printf ( " \n \t " ) ; printf ( " \n \t " ) ; printf ( " \n \t " ) ; printf ( " \n \t " ) ; printf ( " \n " ) ; c = 0 ; option_index = 0 ; while ( c != -1 ) { c = getopt_long ( argc , argv , " " , long_options , & option_index ) ; if ( c == -1 ) break ; switch ( c ) { case ' ' : printf ( " \n " , optarg ) ; strcpy ( gd . hdr_file_name , optarg ) ; gd . hdr_file_flag = 1 ; break ; case ' ' : printf ( " \n " , optarg ) ; strcpy ( gd . entries [ 0 ] . name , optarg ) ; gd . entry1_flag = 1 ; break ; case ' ' : printf ( " \n " , optarg ) ; strcpy ( ap_file , optarg ) ; append_flag = true ; break ; case ' ' : offset = STR_TO_UL ( optarg , 16 ) ; printf ( " \n " , offset ) ; append_flag = true ; break ; default : printf ( " \n " , c ) ; } } if ( gd . help_flag == 1 ) { print_usage ( argv [ 0 ] ) ; return SUCCESS ; } if ( append_flag == true ) { if ( strlen ( ap_file ) == 0 || offset == 0 ) { printf ( " \n " ) ; return FAILURE ; } strcpy ( gd . ap_file [ gd . ap_count ] . name , ap_file ) ; gd . ap_file [ gd . ap_count ] . offset = offset ; gd . ap_count ++ ; } if ( optind != argc - 1 ) { printf ( " \n " ) ; print_usage ( argv [ 0 ] ) ; return FAILURE ; } crypto_print_attribution ( ) ; gd . input_file = argv [ optind ] ; printf ( " \n \n " , gd . input_file ) ; cfg_taal = get_ta_from_file ( gd . input_file ) ; if ( cfg_taal == TA_UNKNOWN_MAX ) { printf ( " \n \n " ) ; return FAILURE ; } ret = taal_parse_input_file ( cfg_taal ) ; if ( ret != SUCCESS ) return ret ; ret = taal_calc_srk_hash ( cfg_taal ) ; if ( ret != SUCCESS ) return ret ; if ( gd . option_srk_hash == 0 ) { ret = taal_fill_structures ( cfg_taal ) ; if ( ret != SUCCESS ) return ret ; ret = taal_calc_img_hash ( cfg_taal ) ; if ( ret != SUCCESS ) return ret ; ret = taal_create_hdr ( cfg_taal ) ; if ( ret != SUCCESS ) return ret ; if ( gd . verbose_flag ) { ret = taal_dump_header ( cfg_taal ) ; if ( ret != SUCCESS ) return ret ; printf ( " \n \n " ) ; for ( i = 0 ; i < SHA256_DIGEST_LENGTH ; i ++ ) printf ( " " , gd . img_hash [ i ] ) ; if ( gd . ie_table_size != 0 ) { printf ( " \n \n " , gd . entries [ 0 ] . addr_low ) ; } } printf ( " \n \n " ) ; if ( gd . option_img_hash == 1 ) { ret = create_img_hash_file ( ) ; if ( ret != SUCCESS ) return ret ; printf ( " \n " , gd . img_hash_file_name ) ; printf ( " \n " ) ; } else { ret = calculate_signature ( ) ; if ( ret != SUCCESS ) return ret ; ret = append_signature ( ) ; if ( ret != SUCCESS ) return ret ; printf ( " \n " ) ; } printf ( " \n \n " ) ; printf ( " \n " , gd . hdr_file_name ) ; } if ( gd . srk_hash_flag == 1 ) { printf ( " \n \n \n " ) ; for ( i = 0 ; i < SHA256_DIGEST_LENGTH ; i ++ ) printf ( " " , gd . srk_hash [ i ] ) ; srk = ( uint32_t * ) gd . srk_hash ; for ( i = 0 ; i < SHA256_DIGEST_LENGTH / sizeof ( uint32_t ) ; i ++ ) printf ( " \n \t " , i , htonl ( srk [ i ] ) ) ; } else { printf ( " \n " ) ; } ret = get_apnd_img ( gd . input_file ) ; if ( ret != SUCCESS ) return ret ; ret = add_apnd_img ( ) ; if ( ret != SUCCESS ) return ret ; printf ( " \n \n " ) ; return SUCCESS ; }
Function     :       create_hdr
 Arguments    :       argc - Argument Count
                      argv - Argumnet List
 Return       :       SUCCESS or FAILURE
 Description  :       Main function where execution starts
Function : create_hdr Arguments : argc - Argument Count argv - Argumnet List Return : SUCCESS or FAILURE Description : Main function where execution starts
/* Check the command line argument */ /* Print the Attribution */ /* Get the Trust Arch Version from Input File */ /* Invalid Platform Name in Input File */ /* TAAL: Parse the Input File and Populate the Global Structure */ /* TAAL: Create and Calculate Public Key / SRK Hash */ /* If SRK Hash Option is Selected, Skip remaining part of Tool */ /* TAAL: Fill the Structures (CSF Header, SG Table) */ /* TAAL: Calculate Image Hash Required for Signature */ /* TAAL: Combine Structures to create the Output Header */ /* TAAL: Dump the header fields */ /* Check if option for img_has is selected */ /* Calculate the Signature over Image Hash */ /* Append Signature to Header File */