Daisuke Nojiri | e1741c5 | 2015-02-09 18:15:17 -0800 | [diff] [blame] | 1 | /* |
| 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 Nojiri | e1741c5 | 2015-02-09 18:15:17 -0800 | [diff] [blame] | 14 | #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 | *---------------------------------------------------------------------*/ |
| 26 | int 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 Nojiri | e1741c5 | 2015-02-09 18:15:17 -0800 | [diff] [blame] | 41 | /*---------------------------------------------------------------------- |
| 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 | *---------------------------------------------------------------------*/ |
| 47 | size_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 Nojiri | e1741c5 | 2015-02-09 18:15:17 -0800 | [diff] [blame] | 57 | /*---------------------------------------------------------------------- |
| 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 | *---------------------------------------------------------------------*/ |
| 65 | int 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 Georgi | 5f771dc | 2016-12-15 14:51:08 +0100 | [diff] [blame^] | 84 | fclose(file); |
Daisuke Nojiri | e1741c5 | 2015-02-09 18:15:17 -0800 | [diff] [blame] | 85 | return -1; |
| 86 | } |
| 87 | fclose(file); |
| 88 | return 0; |
| 89 | } |
| 90 | |
Daisuke Nojiri | e1741c5 | 2015-02-09 18:15:17 -0800 | [diff] [blame] | 91 | /*---------------------------------------------------------------------- |
| 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 | *---------------------------------------------------------------------*/ |
| 99 | int 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 | } |