2 * Utility functions called from fips_fmp_hmac.sh.
4 * executed during Kernel build
6 * Copyright (c) 2014 Samsung Electronics
13 int main(int argc
, char **argv
)
16 printf("\nUsage : \n");
17 printf("fips_fmp_utils -u vmlinux_file hmac_file offset");
18 printf("fips_fmp_utils -g vmlinux_file section_name offset size out_file");
24 if (!strcmp("-u", argv
[1])) {
25 unsigned long offset
= 0;
26 unsigned char *vmlinux_file
= NULL
;
27 unsigned char *hmac_file
= NULL
;
30 printf("\nUsage : \n");
31 printf("fips_fmp_utils -u vmlinux_file hmac_file offset");
37 vmlinux_file
= argv
[2];
39 offset
= atol(argv
[4]);
41 if (!vmlinux_file
|| !hmac_file
|| !offset
) {
42 printf ("./fips_fmp_utils -u vmlinux_file hmac_file offset");
46 return update_fmp_hmac(vmlinux_file
, hmac_file
, offset
);
47 } else if (!strcmp("-g", argv
[1])) {
48 const char *in_file
= NULL
;
49 const char *section_name
= NULL
;
50 unsigned long offset
= 0;
51 unsigned long size
= 0;
52 const char *out_file
= NULL
;
55 printf("\nUsage : \n");
56 printf("./fips_fmp_utils -g vmlinux_file section_name offset size out_file");
63 section_name
= argv
[3];
64 offset
= atol(argv
[4]);
68 if (!in_file
|| !section_name
|| !offset
|| !size
|| !out_file
) {
69 printf("./fips_fmp_utils -g vmlinux_file section_name offset size out_file");
73 return collect_fmp_bytes(in_file
, section_name
, offset
, size
, out_file
);
75 printf("\nUsage : \n");
76 printf("fips_fmp_utils -u vmlinux_file hmac_file offset");
77 printf("fips_fmp_utils -g vmlinux_file section_name offset size out_file");
85 * Given a vmlinux file, dumps "size" bytes from given "offset" to output file
86 * in_file : absolute path to vmlinux file
87 * section_name : Used only for printing / debugging
88 * offset : offset in file from where to dump bytes
89 * size : how many bytes to dump
90 * out_file : Output file, where to dump bytes.
91 * Open in append mode, to keep previous bytes, if present
92 * Caller need to clean up before 1st call
94 * Returns 0, if success
98 int collect_fmp_bytes(const char *in_file
, const char *section_name
, unsigned long offset
,
99 unsigned long size
, const char *out_file
)
104 unsigned char data
= 0;
106 if (!in_file
|| !section_name
|| !offset
|| !size
|| !out_file
) {
107 printf ("collect_fmp_bytes : Invalid arguments");
111 printf("Section : %s\n", section_name
);
113 in_fp
= fopen(in_file
, "r");
115 printf("Unable to open file : %s", in_file
);
119 if (fseek(in_fp
, offset
, SEEK_SET
) != 0) {
120 printf ("Unable to seek file : %s", in_file
);
125 out_fp
= fopen(out_file
, "ab");
127 printf ("Unable to open file : %s", out_file
);
132 for (i
= 1; i
<= size
; i
++) {
133 if ( 1 != fread (&data
, sizeof(unsigned char), 1, in_fp
)) {
134 printf("Unable to read 1 byte from file : %s", in_file
);
140 printf ("%02x ", data
);
141 if (1 != fwrite (&data
, 1, 1, out_fp
)) {
142 printf("Unable to write 1 byte to file : %s", out_file
);
158 #define SHA256_DIGEST_SIZE 32
161 * Given a vmlinux file, overwrites bytes at given offset with hmac bytes, available in
163 * Return 0, if Success
166 int update_fmp_hmac(const char *vmlinux_path
, const char *hmac_path
, unsigned long offset
)
168 FILE *vmlinux_fp
= NULL
;
169 FILE *hmac_fp
= NULL
;
171 unsigned char hmac
[SHA256_DIGEST_SIZE
];
173 if (!vmlinux_path
|| !hmac_path
|| !offset
) {
174 printf("FIPS update_fmp_hmac : Invalid Params");
178 vmlinux_fp
= fopen(vmlinux_path
, "r+b");
180 printf("Unable to open vmlinux file ");
184 hmac_fp
= fopen(hmac_path
, "rb");
187 printf("Unable to open hmac file ");
192 if (SHA256_DIGEST_SIZE
!= fread(&hmac
, sizeof(unsigned char), SHA256_DIGEST_SIZE
, hmac_fp
)) {
193 printf("Unable to read %d bytes from hmac file", SHA256_DIGEST_SIZE
);
199 if (fseek(vmlinux_fp
, offset
, SEEK_SET
) != 0) {
200 printf("Unable to seek into vmlinux file.");
206 if (SHA256_DIGEST_SIZE
!= fwrite (hmac
, sizeof(unsigned char), SHA256_DIGEST_SIZE
, vmlinux_fp
)) {
207 printf("Unable to write %d byte into vmlinux", SHA256_DIGEST_SIZE
);