Files
OpenCellular/host/lib/host_misc.c
Bill Richardson 782990277a Split libvboot_host.a into external and local libraries.
We've been creating and linking against a library called "libvboot_host.a"
for two different reasons. The main purpose is to build the vboot_reference
tools found in the utility/ directory. But there are some external userspace
programs that would also like to use some functions in this library.

This change establishes libvboot_host.a as the library for use by external
userspace programs only, and creates a new libvboot_util.a library that's
only used inside this source tree to build the vboot utilities.

BUG=chromium:231567
BRANCH=ToT
TEST=manual

Build and run the local tests:

  make runalltests
  make clean

Build Link firmware and all the utilities:

  emerge-link chromeos-base/vboot_reference \
              sys-boot/depthcharge \
              sys-boot/coreboot \
              chromeos-base/chromeos-ec \
              chromeos-base/chromeos-firmware-link \
              chromeos-base/chromeos-cryptohome \
              chromeos-base/update_engine \
              chromeos-base/chromeos-installer \
              chromeos-base/chromeos-login \
              chromeos-base/verity

Build Lumpy utilities, which include the 32-bit cros_installer:

  emerge-lumpy chromeos-base/vboot_reference \
               chromeos-base/chromeos-login \
               chromeos-base/verity \
               chromeos-base/update_engine \
               chromeos-base/chromeos-installer \
               chromeos-base/chromeos-cryptohome

Change-Id: Ie81ff1f74a6356cb8fab7d98471139d7758c4f19
Signed-off-by: Bill Richardson <wfrichar@chromium.org>
Reviewed-on: https://chromium-review.googlesource.com/207016
Reviewed-by: Randall Spangler <rspangler@chromium.org>
2014-07-09 01:30:48 +00:00

117 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) {
char buf[64];
int value;
char* e = NULL;
if (!ReadFileString(buf, sizeof(buf), filename))
return -1;
/* Convert to integer. Allow characters after the int ("123 blah"). */
value = strtol(buf, &e, 0);
if (e == buf)
return -1; /* No characters consumed, so conversion failed */
return value;
}
int ReadFileBit(const char* filename, int bitmask) {
int value = ReadFileInt(filename);
if (value == -1)
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;
}