blob: f5be50afc77df556c77882923989a3aef79b7a62 [file] [log] [blame]
Daisuke Nojirie1741c52015-02-09 18:15:17 -08001/*
2 * Copyright (C) 2015 Broadcom Corporation
3 *
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public License as
6 * published by the Free Software Foundation version 2.
7 *
8 * This program is distributed "as is" WITHOUT ANY WARRANTY of any
9 * kind, whether express or implied; without even the implied warranty
10 * of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
12 */
13
Daisuke Nojirie1741c52015-02-09 18:15:17 -080014#include <stdio.h>
15#include <string.h>
16#include "secimage.h"
17
18/*----------------------------------------------------------------------
19 * Name : ReadBinaryFile
20 * Purpose : Read some data from file of raw binary
21 * Input : fname : file to be read
22 * buf : buffer which is the data desitnation
23 * maxlen : maiximum length of data to be read
24 * Output : none
25 *---------------------------------------------------------------------*/
26int ReadBinaryFile(char *fname, uint8_t *buf, int maxlen)
27{
28 FILE *fp = NULL;
29 int len = 0;
30
31 fp = fopen(fname, "rb");
32 if (fp == NULL)
33 return 0;
34 printf("fname=%s, len=%d\n", fname, maxlen);
35 len = fread(buf, 1, maxlen, fp);
36 fclose(fp);
37
38 return len;
39}
40
Daisuke Nojirie1741c52015-02-09 18:15:17 -080041/*----------------------------------------------------------------------
42 * Name : FileSizeGet
43 * Purpose : Return the size of the file
44 * Input : file: FILE * to the file to be processed
45 * Output : none
46 *---------------------------------------------------------------------*/
47size_t FileSizeGet(FILE *file)
48{
49 long length;
50
51 fseek(file, 0, SEEK_END);
52 length = ftell(file);
53 rewind(file);
54 return (size_t)length;
55}
56
Daisuke Nojirie1741c52015-02-09 18:15:17 -080057/*----------------------------------------------------------------------
58 * Name : DataRead
59 * Purpose : Read all the data from a file
60 * Input : filename : file to be read
61 * buf : buffer which is the data destination
62 * length : length of data to be read
63 * Output : none
64 *---------------------------------------------------------------------*/
65int DataRead(char *filename, uint8_t *buf, int *length)
66{
67 FILE *file;
68 int len = *length;
69
70 file = fopen(filename, "rb");
71 if (file == NULL) {
72 printf("Unable to open file: %s\n", filename);
73 return -1;
74 }
75 len = FileSizeGet(file);
Patrick Georgica801962016-12-15 14:53:23 +010076 if (len < 0) {
77 printf("Unable to seek in file: %s\n", filename);
78 fclose(file);
79 return -1;
80 }
Daisuke Nojirie1741c52015-02-09 18:15:17 -080081 if (len < *length)
82 *length = len;
83 else
84 /* Do not exceed the maximum length of the buffer */
85 len = *length;
86 if (fread((uint8_t *)buf, 1, len, file) != len) {
87 printf("Error reading data (%d bytes) from file: %s\n",
88 len, filename);
Patrick Georgi5f771dc2016-12-15 14:51:08 +010089 fclose(file);
Daisuke Nojirie1741c52015-02-09 18:15:17 -080090 return -1;
91 }
92 fclose(file);
93 return 0;
94}
95
Daisuke Nojirie1741c52015-02-09 18:15:17 -080096/*----------------------------------------------------------------------
97 * Name : DataWrite
98 * Purpose : Write some binary data to a file
99 * Input : filename : file to be written
100 * buf : buffer which is the data source
101 * length : length of data to be written
102 * Output : none
103 *---------------------------------------------------------------------*/
104int DataWrite(char *filename, char *buf, int length)
105{
106 FILE *file;
107
108 file = fopen(filename, "wb");
109 if (file == NULL) {
110 printf("Unable to open output file %s\n", filename);
111 return -1;
112 }
113 if (fwrite(buf, 1, length, file) < length) {
114 printf("Unable to write %d bytes to output file %s (0x%X).\n",
115 length, filename, ferror(file));
116 fclose(file);
117 return -1;
118 }
119
120 fflush(file);
121 fclose(file);
122 return 0;
123}