xref: /utopia/UTPA2-700.0.x/projects/build/scripts/packramfs.c (revision 53ee8cc121a030b8d368113ac3e966b4705770ef)
1 #include <stdio.h>
2 #include <unistd.h>
3 #include <string.h>
4 #include <sys/types.h>
5 #include <sys/stat.h>
6 #include <fcntl.h>
7 #include <stdlib.h>
8 
9 #define BLOCK_SIZE  32768
10 
11 typedef struct _ST_INITRAMFS_HEAD
12 {
13 	unsigned int initramfs_start;
14 	unsigned int initramfs_end;
15 } ST_INITRAMFS_HEAD;
16 
main(int argc,char * argv[])17 int main( int argc, char *argv[] )
18 {
19 	ST_INITRAMFS_HEAD stHead;
20 
21 	FILE * fp_system_map;
22 
23 	FILE * fp_in;
24 	FILE * fp_out;
25 
26 	unsigned addr;
27 	char str0[32];
28 	char str1[32];
29 
30 	void *buf;
31 	int offset;
32 	size_t tmp;
33 
34 	unsigned int ker_dst_base = 0;
35 	unsigned int ker_dst_entry = 0;
36 	unsigned int ker_dst_end = 0;
37 
38 	char cmd_str[512];
39 
40 	if ( argc < 2 ) {
41 		printf( "Usage: %s [FILE]\n", argv[0] );
42 		return -1;
43 	}
44 
45 	fp_out = fopen( argv[1], "wb+" );
46 	if ( fp_out == NULL ) {
47 		perror( argv[1] );
48 		return -1;
49 	}
50 
51 	fp_in = fopen( "usr/initramfs_data.cpio.gz", "rb" );
52 	if ( fp_in == NULL ) {
53 		perror( "usr/initramfs_data.cpio.gz" );
54 		fclose( fp_out );
55 		return -1;
56 	}
57 
58 #ifdef CONFIG_INITRAMFS_IMG_HDR_ADDR
59 	stHead.initramfs_start = CONFIG_INITRAMFS_IMG_HDR_ADDR + sizeof( ST_INITRAMFS_HEAD );
60 #else
61 	stHead.initramfs_start = 0x88000000 + sizeof( ST_INITRAMFS_HEAD );
62 #endif
63 
64 	fseek( fp_in, 0, SEEK_END );
65 
66 	offset = ftell( fp_in );
67 	if ( offset >= 0 ) {	// real image size
68 		stHead.initramfs_end = (unsigned int) (stHead.initramfs_start + offset);
69 	} else {
70 		stHead.initramfs_end = stHead.initramfs_start;
71 	}
72 	fwrite( &stHead, 1, sizeof( ST_INITRAMFS_HEAD ), fp_out );
73 
74 	rewind( fp_in );
75 	buf = malloc( 0x400000 );
76 	if ( buf == NULL ) {
77 		perror( "malloc fail" );
78 		return -1;
79 	}
80 
81 	while ( !feof( fp_in ) ) {
82 		tmp = fread( buf, 1, 0x400000, fp_in );
83 		if ( tmp <= 0 ) {
84 			perror( "read fail" );
85 			free( buf );
86 			fclose( fp_in );
87 			fclose( fp_out );
88 			return -1;
89 		}
90 		fwrite( buf, 1, tmp, fp_out );
91 		stHead.initramfs_end -= tmp;
92 	}
93 
94 	free( buf );
95 
96 	fclose( fp_in );
97 	fclose( fp_out );
98 
99 	if ( stHead.initramfs_end < stHead.initramfs_start ) {
100 		printf( "cannot read overall file.\n" );
101 		return -1;
102 	}
103 
104 	//! find data from system map
105 	fp_system_map = fopen( "System.map", "r" );
106 	while( fscanf( fp_system_map, "%x %s %s", &addr, str0, str1 ) > 0 ) {
107 		if( strcmp( str1, "_text" ) == 0 ) {
108 			ker_dst_base = addr;
109 		} else if( strcmp( str1, "kernel_entry" ) == 0 ) {
110 			ker_dst_entry = addr;
111 		} else if( strcmp( str1, "__initramfs_start" ) == 0 ) {
112 			ker_dst_end = addr;
113 		}
114 	}
115 	fclose( fp_system_map );
116 
117 	printf( "[CREATE VMLINUX IMAGE]\r\n" );
118 	system( "cp arch/mips/boot/vmlinux.bin vmlinux.bin" );
119 	system( "gzip -f vmlinux.bin > vmlinux.bin.gz" );
120 	memset( cmd_str, 0, 512 );
121 	sprintf( cmd_str, "mkimage -n 'linux-3.0.20' -A mips -O linux -T kernel -C gzip -a 0x%08X -e 0x%08X -d vmlinux.bin.gz vmlinux.img", (unsigned int)ker_dst_base, (unsigned int)ker_dst_entry );
122 	system( cmd_str );
123 	system( "rm -f vmlinux.bin.gz" );
124 	printf( "\r\n");
125 
126 	printf( "[CREATE INITRAMFS IMAGE]\r\n" );
127 	memset( cmd_str, 0, 512 );
128 	system( "chmod 644 initramfs.bin" );
129 	sprintf( cmd_str, "mkimage -n 'initramfs' -A mips -O linux -T ramdisk -C none -a 0x%08X -d initramfs.bin initramfs.img", stHead.initramfs_start - sizeof( ST_INITRAMFS_HEAD ) );
130 	system( cmd_str );
131 	printf( "\r\n");
132 
133 	return 0;
134 }
135