mirror of
https://github.com/Telecominfraproject/OpenCellular.git
synced 2025-11-24 10:14:55 +00:00
Currently ReadFileInt assumes that an integer value read from a file is never going to be "-1" and uses that value to indicate failure. In particular for GPIO values that may be returned by the kernel it is possible for them to be not simply 0 or 1 but instead a bit within the GPIO status register that indicates the value. The function semantics are changed to have the caller pass in the variable to store the integer in, and use the return code explicitly as a pass or fail condition. This requires all the callers of ReadFileInt to be changed to use the new scheme, and the x86 ReadGpio function is changed to normalize the GPIO value that is read from the kernel instead of assuming it is always 1 for active high values. BUG=chrome-os-partner:32645 BRANCH=samus,auron TEST=build for samus, check crossystem output and ensure that all values are properly reported and that wpsw_cur is correct now. Also tested to ensure no changes in output on: x86-alex, daisy, peach_pit, lumpy, stumpy, nyan_big, nyan_blaze, rush_ryu, panther, wolf, zako, auron, rambi, squawks, parrot_ivb, veyron_pinky Change-Id: I824152eed5f96cf1faaa18ba31a01f4d346ad172 Signed-off-by: Duncan Laurie <dlaurie@chromium.org> Reviewed-on: https://chromium-review.googlesource.com/223009 Reviewed-by: Bill Richardson <wfrichar@chromium.org>
116 lines
2.2 KiB
C
116 lines
2.2 KiB
C
/* Copyright (c) 2011 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.
|
|
*
|
|
* Host functions for verified boot.
|
|
*/
|
|
|
|
/* TODO: change all 'return 0', 'return 1' into meaningful return codes */
|
|
|
|
#include <stdio.h>
|
|
#include <stdlib.h>
|
|
#include <string.h>
|
|
#include <unistd.h>
|
|
|
|
#include "cryptolib.h"
|
|
#include "host_common.h"
|
|
#include "vboot_common.h"
|
|
|
|
|
|
char* StrCopy(char* dest, const char* src, int dest_size) {
|
|
strncpy(dest, src, dest_size);
|
|
dest[dest_size - 1] = '\0';
|
|
return dest;
|
|
}
|
|
|
|
|
|
uint8_t* ReadFile(const char* filename, uint64_t* sizeptr) {
|
|
FILE* f;
|
|
uint8_t* buf;
|
|
uint64_t size;
|
|
|
|
f = fopen(filename, "rb");
|
|
if (!f) {
|
|
VBDEBUG(("Unable to open file %s\n", filename));
|
|
return NULL;
|
|
}
|
|
|
|
fseek(f, 0, SEEK_END);
|
|
size = ftell(f);
|
|
rewind(f);
|
|
|
|
buf = malloc(size);
|
|
if (!buf) {
|
|
fclose(f);
|
|
return NULL;
|
|
}
|
|
|
|
if(1 != fread(buf, size, 1, f)) {
|
|
VBDEBUG(("Unable to read from file %s\n", filename));
|
|
fclose(f);
|
|
free(buf);
|
|
return NULL;
|
|
}
|
|
|
|
fclose(f);
|
|
if (sizeptr)
|
|
*sizeptr = size;
|
|
return buf;
|
|
}
|
|
|
|
|
|
char* ReadFileString(char* dest, int size, const char* filename) {
|
|
char* got;
|
|
FILE* f;
|
|
|
|
f = fopen(filename, "rt");
|
|
if (!f)
|
|
return NULL;
|
|
|
|
got = fgets(dest, size, f);
|
|
fclose(f);
|
|
return got;
|
|
}
|
|
|
|
|
|
int ReadFileInt(const char* filename, unsigned* value) {
|
|
char buf[64];
|
|
char* e = NULL;
|
|
|
|
if (!ReadFileString(buf, sizeof(buf), filename))
|
|
return -1;
|
|
|
|
/* Convert to integer. Allow characters after the int ("123 blah"). */
|
|
*value = (unsigned)strtoul(buf, &e, 0);
|
|
if (e == buf)
|
|
return -1; /* No characters consumed, so conversion failed */
|
|
|
|
return 0;
|
|
}
|
|
|
|
|
|
int ReadFileBit(const char* filename, int bitmask) {
|
|
unsigned value;
|
|
if (ReadFileInt(filename, &value) < 0)
|
|
return -1;
|
|
else return (value & bitmask ? 1 : 0);
|
|
}
|
|
|
|
|
|
int WriteFile(const char* filename, const void *data, uint64_t size) {
|
|
FILE *f = fopen(filename, "wb");
|
|
if (!f) {
|
|
VBDEBUG(("Unable to open file %s\n", filename));
|
|
return 1;
|
|
}
|
|
|
|
if (1 != fwrite(data, size, 1, f)) {
|
|
VBDEBUG(("Unable to write to file %s\n", filename));
|
|
fclose(f);
|
|
unlink(filename); /* Delete any partial file */
|
|
}
|
|
|
|
fclose(f);
|
|
return 0;
|
|
}
|