⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 coff_both.c

📁 5402hpi自举加载的程序源码欢迎下载
💻 C
📖 第 1 页 / 共 2 页
字号:
     * Get section header
     */
    headersize = sizeof(sectheader1);
									/* 'headersize' is assigned the size of
										the structure sectheader1 */

    printf("\nSection\t\tAddress\t\tSize\tPage\n");
    printf("---------------------------------------------\n");
    fflush(stdout);

    for(i=1; i<= fileheader.sections;i++) {
									/* for the total number of sections in the
										input file do the following */
	file2struct_ptr = (unsigned char *)&sectheader1;
									/* assigns the address of 'sectheader1' to
										'file2struct_ptr' ;
										'sectheader1' is a structure of type 
										'struct sheader' */

	fread(file2struct_ptr, headersize,1, infile);
									/* 'file2struct_ptr' reads 'headersize'
										number of bytes from 'infile' */

//        swap_sheader(&sectheader1); /* Fix the endian problem */
	
	strncpy(sectionname,sectheader1.name,8);
									/* copy 8 characters from the 'name' field
										of struct 'sectheader1' into file 
										'sectionname' */
	strncat(sectionname,"\0",1);	/* terminate the string with a null char */
	if(sectheader1.rawdataptr == 0x0) {
	    sectheader1.sectsize =0;		/* determining section size */
	}
	if ((!strcmp(sectheader1.name,".text")) && i==1){
	    entryaddr= sectheader1.phyaddr;}
									/* enter loop if it is the first iteration
									   and if the name of the section is 
									   ".text" */

	currentloc = sizeof(fileheader) + fileheader.optbytes 
	    + (i* sizeof(sectheader1)) - 2;
									/* this covers:
									  - the fileheader length,
									  - the optional fileheader length
									  - the length of the sectheader1 for the 
										number of sections read 
										(reflected by 'i' value of loop)
									  - (-2)
									to give the current section ptr location
									*/


	file2struct_ptr = (unsigned char *)&inbuff;
									/* file2struct_ptr points to structure 
									inbuff of type struct buffer */
	fseek(infile,sectheader1.rawdataptr,SEEK_SET);
	sectlen=0;
	printf("%s\t\t",sectionname);
	printf("%4.4xh\t\t", sectheader1.phyaddr);
	printf("%4.4xh\t", sectheader1.sectsize);
	if((int)sectheader1.page == 0){printf("PROG\n");}
	else{printf("DATA\n");}

	/* 
	 * Print the content of each section.
	 */
	if (sectheader1.sectsize > 0 && strcmp(sectheader1.name,".xref")) {
									/* If the sectionsize is non-zero and
									if the sectionname is not ".xref"
									then enter the loop */

	    if (out) {
		fprintf(ofp, "Section = %s\n", sectionname);
									/* recall, "ofp" is a pointer to the 
									opened file 'targetfile.c' with write 
									option */
	    }
	    for(; sectlen < sectheader1.sectsize; ) {
		remaining =sectheader1.sectsize - sectlen;
		if(remaining >= HPIbuffsize){
		    fread(file2struct_ptr, HPIbuffsize *2, 1, infile);
									/* read contents of targetfile and store
									them in buffer "file2struct_ptr"...since
									this is a pointer to inbuff then the 
									contents are stored in inbuff */
		    DownLoad(HPIbuf,HPIbuffsize,
			     (int)(sectheader1.phyaddr+sectlen),
			     sectheader1.page, inbuff, ofp);
									/* array inbuff is an argument for
									function Download */
		    remaining=HPIbuffsize;
		}
		else{
		    fread(file2struct_ptr,(size_t)remaining*2,1, infile);
		    DownLoad(HPIbuf,(int)remaining,
			     (int)(sectheader1.phyaddr+sectlen),
			     sectheader1.page, inbuff, ofp);
		}
		sectlen +=remaining;
	    }
	}
	fseek(infile,currentloc, SEEK_SET);
	fflush(stdout);
    }
#if 0
    if(strlen(entrypt) >= 1){
	if((tempentry =Find_Label(infile, entrypt, fileheader)) == -1){
	    printf("\n\"%s\" label not found...default to .text section\n",entrypt);}
	else{entryaddr= tempentry;}
    }
#endif
    printf("\nEntry Point is: %4.4x\n\n", entryaddr);

    fclose(infile);
    if (out) {
	printf("Output file is %s\n", ofname);
	fclose(ofp);
    }
    return(entryaddr);
	}

	else if (value==2) {
		
    /*
     * Get File header.
     */
    file2struct_ptr = (unsigned char *)&fileheader;
									/* assigns the address of 'fileheader' to
										file2struct_ptr ;
										'fileheader' is a structure of type 
										'struct fheader' */
    headersize = sizeof(fileheader);
									/* headersize is assigned the size of 
									   structure 'fileheader' */
    
	if((infile = fopen(targetfile, "rb")) == NULL) {
									/* if 'targetfile' does not exist then,
										print error */
	printf("Cannot open %s file\n", targetfile); 
	return(-1);
    }

    fread(file2struct_ptr, headersize,1,infile);
									/* the 'file2struct_ptr' will read 1 entry
										of 'headersize' bytes from  the file 
										'infile' ;*/

//    swap_fileheader(&fileheader);
    printf("App File: %s   %s", targetfile, ctime(&fileheader.stamp));
									/* prompt info while invoking utility */

    /*
     * Skip optional header, if any.
     */

	printf("Optional header = %d \n", fileheader.optbytes);
    if(fileheader.optbytes) {
	fseek(infile,fileheader.optbytes-2, SEEK_CUR);
									/* Set the file position for "infile" to
										optbytes-2 characters from where it 
										was left off by the previous fread 
										statement */
	printf("COFF (.out) file\n");
    }
    /* 
     * Get section header
     */
    headersize = sizeof(sectheader2);
									/* 'headersize' is assigned the size of
										the structure sectheader2 */

    printf("\nSection\t\tAddress\t\tSize\tPage\n");
    printf("---------------------------------------------\n");
    fflush(stdout);

    for(i=1; i<= fileheader.sections;i++) {
									/* for the total number of sections in the
										input file do the following */
	file2struct_ptr = (unsigned char *)&sectheader2;
									/* assigns the address of 'sectheader2' to
										'file2struct_ptr' ;
										'sectheader2' is a structure of type 
										'struct sheader' */

	fread(file2struct_ptr, headersize,1, infile);
									/* 'file2struct_ptr' reads 'headersize'
										number of bytes from 'infile' */

//        swap_sheader(&sectheader2); /* Fix the endian problem */
	
	strncpy(sectionname,sectheader2.name,8);
									/* copy 8 characters from the 'name' field
										of struct 'sectheader2' into file 
										'sectionname' */
	strncat(sectionname,"\0",1);	/* terminate the string with a null char */
	if(sectheader2.rawdataptr == 0x0) {
	    sectheader2.sectsize =0;		/* determining section size */
	}
	if ((!strcmp(sectheader2.name,".text")) && i==1){
	    entryaddr= sectheader2.phyaddr;}
									/* enter loop if it is the first iteration
									   and if the name of the section is 
									   ".text" */

	currentloc = sizeof(fileheader) + fileheader.optbytes 
	    + (i* sizeof(sectheader2)) - 2;
									/* this covers:
									  - the fileheader length,
									  - the optional fileheader length
									  - the length of the sectheader2 for the 
										number of sections read 
										(reflected by 'i' value of loop)
									  - (-2)
									to give the current section ptr location
									*/


	file2struct_ptr = (unsigned char *)&inbuff;
									/* file2struct_ptr points to structure 
									inbuff of type struct buffer */
	fseek(infile,sectheader2.rawdataptr,SEEK_SET);
	sectlen=0;
	printf("%s\t\t",sectionname);
	printf("%4.4xh\t\t", sectheader2.phyaddr);
	printf("%4.4xh\t", sectheader2.sectsize);
	if((int)sectheader2.page == 0){printf("PROG\n");}
	else{printf("DATA\n");}

	/* 
	 * Print the content of each section.
	 */
	if (sectheader2.sectsize > 0 && strcmp(sectheader2.name,".xref")) {
									/* If the sectionsize is non-zero and
									if the sectionname is not ".xref"
									then enter the loop */

	    if (out) {
		fprintf(ofp, "Section = %s\n", sectionname);
									/* recall, "ofp" is a pointer to the 
									opened file 'targetfile.c' with write 
									option */
	    }
	    for(; sectlen < sectheader2.sectsize; ) {
		remaining =sectheader2.sectsize - sectlen;
		if(remaining >= HPIbuffsize){
		    fread(file2struct_ptr, HPIbuffsize *2, 1, infile);
									/* read contents of targetfile and store
									them in buffer "file2struct_ptr"...since
									this is a pointer to inbuff then the 
									contents are stored in inbuff */
		    DownLoad(HPIbuf,HPIbuffsize,
			     (int)(sectheader2.phyaddr+sectlen),
			     sectheader2.page, inbuff, ofp);
									/* array inbuff is an argument for
									function Download */
		    remaining=HPIbuffsize;
		}
		else{
		    fread(file2struct_ptr,(size_t)remaining*2,1, infile);
		    DownLoad(HPIbuf,(int)remaining,
			     (int)(sectheader2.phyaddr+sectlen),
			     sectheader2.page, inbuff, ofp);
		}
		sectlen +=remaining;
	    }
	}
	fseek(infile,currentloc, SEEK_SET);
	fflush(stdout);
    }
#if 0
    if(strlen(entrypt) >= 1){
	if((tempentry =Find_Label(infile, entrypt, fileheader)) == -1){
	    printf("\n\"%s\" label not found...default to .text section\n",entrypt);}
	else{entryaddr= tempentry;}
    }
#endif
    printf("\nEntry Point is: %4.4x\n\n", entryaddr);

    fclose(infile);
    if (out) {
	printf("Output file is %s\n", ofname);
	fclose(ofp);
    }
    return(entryaddr);
	}



}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -