blob: 4d99aad8eaaa9016cff6600215f74723c1417617 [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
14
15#include <stdio.h>
16#include <string.h>
17#include "secimage.h"
18
19/*----------------------------------------------------------------------
20 * Name : ReadBinaryFile
21 * Purpose : Read some data from file of raw binary
22 * Input : fname : file to be read
23 * buf : buffer which is the data desitnation
24 * maxlen : maiximum length of data to be read
25 * Output : none
26 *---------------------------------------------------------------------*/
27int ReadBinaryFile(char *fname, uint8_t *buf, int maxlen)
28{
29 FILE *fp = NULL;
30 int len = 0;
31
32 fp = fopen(fname, "rb");
33 if (fp == NULL)
34 return 0;
35 printf("fname=%s, len=%d\n", fname, maxlen);
36 len = fread(buf, 1, maxlen, fp);
37 fclose(fp);
38
39 return len;
40}
41
42
43/*----------------------------------------------------------------------
44 * Name : FileSizeGet
45 * Purpose : Return the size of the file
46 * Input : file: FILE * to the file to be processed
47 * Output : none
48 *---------------------------------------------------------------------*/
49size_t FileSizeGet(FILE *file)
50{
51 long length;
52
53 fseek(file, 0, SEEK_END);
54 length = ftell(file);
55 rewind(file);
56 return (size_t)length;
57}
58
59
60/*----------------------------------------------------------------------
61 * Name : DataRead
62 * Purpose : Read all the data from a file
63 * Input : filename : file to be read
64 * buf : buffer which is the data destination
65 * length : length of data to be read
66 * Output : none
67 *---------------------------------------------------------------------*/
68int DataRead(char *filename, uint8_t *buf, int *length)
69{
70 FILE *file;
71 int len = *length;
72
73 file = fopen(filename, "rb");
74 if (file == NULL) {
75 printf("Unable to open file: %s\n", filename);
76 return -1;
77 }
78 len = FileSizeGet(file);
79 if (len < *length)
80 *length = len;
81 else
82 /* Do not exceed the maximum length of the buffer */
83 len = *length;
84 if (fread((uint8_t *)buf, 1, len, file) != len) {
85 printf("Error reading data (%d bytes) from file: %s\n",
86 len, filename);
87 return -1;
88 }
89 fclose(file);
90 return 0;
91}
92
93
94/*----------------------------------------------------------------------
95 * Name : DataWrite
96 * Purpose : Write some binary data to a file
97 * Input : filename : file to be written
98 * buf : buffer which is the data source
99 * length : length of data to be written
100 * Output : none
101 *---------------------------------------------------------------------*/
102int DataWrite(char *filename, char *buf, int length)
103{
104 FILE *file;
105
106 file = fopen(filename, "wb");
107 if (file == NULL) {
108 printf("Unable to open output file %s\n", filename);
109 return -1;
110 }
111 if (fwrite(buf, 1, length, file) < length) {
112 printf("Unable to write %d bytes to output file %s (0x%X).\n",
113 length, filename, ferror(file));
114 fclose(file);
115 return -1;
116 }
117
118 fflush(file);
119 fclose(file);
120 return 0;
121}