blob: d71065c9c400a52436051998690dd3bbfa590ced [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);
76 if (len < *length)
77 *length = len;
78 else
79 /* Do not exceed the maximum length of the buffer */
80 len = *length;
81 if (fread((uint8_t *)buf, 1, len, file) != len) {
82 printf("Error reading data (%d bytes) from file: %s\n",
83 len, filename);
Patrick Georgi5f771dc2016-12-15 14:51:08 +010084 fclose(file);
Daisuke Nojirie1741c52015-02-09 18:15:17 -080085 return -1;
86 }
87 fclose(file);
88 return 0;
89}
90
Daisuke Nojirie1741c52015-02-09 18:15:17 -080091/*----------------------------------------------------------------------
92 * Name : DataWrite
93 * Purpose : Write some binary data to a file
94 * Input : filename : file to be written
95 * buf : buffer which is the data source
96 * length : length of data to be written
97 * Output : none
98 *---------------------------------------------------------------------*/
99int DataWrite(char *filename, char *buf, int length)
100{
101 FILE *file;
102
103 file = fopen(filename, "wb");
104 if (file == NULL) {
105 printf("Unable to open output file %s\n", filename);
106 return -1;
107 }
108 if (fwrite(buf, 1, length, file) < length) {
109 printf("Unable to write %d bytes to output file %s (0x%X).\n",
110 length, filename, ferror(file));
111 fclose(file);
112 return -1;
113 }
114
115 fflush(file);
116 fclose(file);
117 return 0;
118}