mirror of
https://github.com/Telecominfraproject/OpenCellular.git
synced 2026-01-09 00:51:29 +00:00
ectool.c has gotten monstrously huge. Refactor out some utility functions. This is precursor work to refactoring out a lower-level flash read/write interface. BUG=chrome-os-partner:20571 BRANCH=none TEST=ectool flashread 0x20000 0x80 /tmp foo -> works Change-Id: I26dae609a73e54e8adaec56edbdce6a0bb4b8758 Signed-off-by: Randall Spangler <rspangler@chromium.org> Reviewed-on: https://gerrit.chromium.org/gerrit/60265 Reviewed-by: Vic Yang <victoryang@chromium.org>
84 lines
1.4 KiB
C
84 lines
1.4 KiB
C
/* Copyright (c) 2013 The Chromium OS Authors. All rights reserved.
|
|
* Use of this source code is governed by a BSD-style license that can be
|
|
* found in the LICENSE file.
|
|
*/
|
|
|
|
#include <ctype.h>
|
|
#include <stdint.h>
|
|
#include <stdio.h>
|
|
#include <stdlib.h>
|
|
#include <string.h>
|
|
|
|
#include "misc_util.h"
|
|
|
|
int write_file(const char *filename, const char *buf, int size)
|
|
{
|
|
FILE *f;
|
|
int i;
|
|
|
|
/* Write to file */
|
|
f = fopen(filename, "wb");
|
|
if (!f) {
|
|
perror("Error opening output file");
|
|
return -1;
|
|
}
|
|
i = fwrite(buf, 1, size, f);
|
|
fclose(f);
|
|
if (i != size) {
|
|
perror("Error writing to file");
|
|
return -1;
|
|
}
|
|
|
|
return 0;
|
|
}
|
|
|
|
char *read_file(const char *filename, int *size)
|
|
{
|
|
FILE *f = fopen(filename, "rb");
|
|
char *buf;
|
|
int i;
|
|
|
|
if (!f) {
|
|
perror("Error opening input file");
|
|
return NULL;
|
|
}
|
|
|
|
fseek(f, 0, SEEK_END);
|
|
*size = ftell(f);
|
|
rewind(f);
|
|
if (*size > 0x100000) {
|
|
fprintf(stderr, "File seems unreasonably large\n");
|
|
fclose(f);
|
|
return NULL;
|
|
}
|
|
|
|
buf = (char *)malloc(*size);
|
|
if (!buf) {
|
|
fprintf(stderr, "Unable to allocate buffer.\n");
|
|
fclose(f);
|
|
return NULL;
|
|
}
|
|
|
|
printf("Reading %d bytes from %s...\n", *size, filename);
|
|
i = fread(buf, 1, *size, f);
|
|
fclose(f);
|
|
if (i != *size) {
|
|
perror("Error reading file");
|
|
free(buf);
|
|
return NULL;
|
|
}
|
|
|
|
return buf;
|
|
}
|
|
|
|
int is_string_printable(const char *buf)
|
|
{
|
|
while (*buf) {
|
|
if (!isprint(*buf))
|
|
return 0;
|
|
buf++;
|
|
}
|
|
|
|
return 1;
|
|
}
|