xref: /freebsd-src/lib/libsecureboot/readfile.c (revision 1d386b48a555f61cb7325543adbbb5c3f3407a66)
15fff9558SSimon J. Gerraty /*-
25fff9558SSimon J. Gerraty  * Copyright (c) 2017-2018, Juniper Networks, Inc.
35fff9558SSimon J. Gerraty  *
45fff9558SSimon J. Gerraty  * Redistribution and use in source and binary forms, with or without
55fff9558SSimon J. Gerraty  * modification, are permitted provided that the following conditions
65fff9558SSimon J. Gerraty  * are met:
75fff9558SSimon J. Gerraty  * 1. Redistributions of source code must retain the above copyright
85fff9558SSimon J. Gerraty  *    notice, this list of conditions and the following disclaimer.
95fff9558SSimon J. Gerraty  * 2. Redistributions in binary form must reproduce the above copyright
105fff9558SSimon J. Gerraty  *    notice, this list of conditions and the following disclaimer in the
115fff9558SSimon J. Gerraty  *    documentation and/or other materials provided with the distribution.
125fff9558SSimon J. Gerraty  *
135fff9558SSimon J. Gerraty  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
145fff9558SSimon J. Gerraty  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
155fff9558SSimon J. Gerraty  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
165fff9558SSimon J. Gerraty  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
175fff9558SSimon J. Gerraty  * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
185fff9558SSimon J. Gerraty  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
195fff9558SSimon J. Gerraty  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
205fff9558SSimon J. Gerraty  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
215fff9558SSimon J. Gerraty  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
225fff9558SSimon J. Gerraty  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
235fff9558SSimon J. Gerraty  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
245fff9558SSimon J. Gerraty  */
255fff9558SSimon J. Gerraty #include <sys/cdefs.h>
265fff9558SSimon J. Gerraty #include <libsecureboot.h>
275fff9558SSimon J. Gerraty 
285fff9558SSimon J. Gerraty unsigned char *
read_fd(int fd,size_t len)29f9510887SSimon J. Gerraty read_fd(int fd, size_t len)
305fff9558SSimon J. Gerraty {
31f9510887SSimon J. Gerraty 	int m, n, x;
325fff9558SSimon J. Gerraty 	unsigned char *buf;
335fff9558SSimon J. Gerraty 
34f9510887SSimon J. Gerraty 	buf = malloc(len + 1);
35*66655411SSimon J. Gerraty 	if (buf != NULL) {
36f9510887SSimon J. Gerraty 		for (x = 0, m = len; m > 0; ) {
375fff9558SSimon J. Gerraty 			n = read(fd, &buf[x], m);
385fff9558SSimon J. Gerraty 			if (n < 0)
395fff9558SSimon J. Gerraty 				break;
405fff9558SSimon J. Gerraty 			if (n > 0) {
415fff9558SSimon J. Gerraty 				m -= n;
425fff9558SSimon J. Gerraty 				x += n;
435fff9558SSimon J. Gerraty 			}
445fff9558SSimon J. Gerraty 		}
455fff9558SSimon J. Gerraty 		if (m == 0) {
46f9510887SSimon J. Gerraty 			buf[len] = '\0';
475fff9558SSimon J. Gerraty 			return (buf);
485fff9558SSimon J. Gerraty 		}
495fff9558SSimon J. Gerraty 		free(buf);
50*66655411SSimon J. Gerraty 	}
515fff9558SSimon J. Gerraty 	return (NULL);
525fff9558SSimon J. Gerraty }
53f9510887SSimon J. Gerraty 
54f9510887SSimon J. Gerraty unsigned char *
read_file(const char * path,size_t * len)55f9510887SSimon J. Gerraty read_file(const char *path, size_t *len)
56f9510887SSimon J. Gerraty {
57f9510887SSimon J. Gerraty 	struct stat st;
58f9510887SSimon J. Gerraty 	unsigned char *ucp;
59f9510887SSimon J. Gerraty 	int fd;
60f9510887SSimon J. Gerraty 
61f9510887SSimon J. Gerraty     	if (len)
62f9510887SSimon J. Gerraty 		*len = 0;
63f9510887SSimon J. Gerraty 	if ((fd = open(path, O_RDONLY)) < 0)
64f9510887SSimon J. Gerraty 		return (NULL);
65f9510887SSimon J. Gerraty 	fstat(fd, &st);
66f9510887SSimon J. Gerraty 	ucp = read_fd(fd, st.st_size);
67f9510887SSimon J. Gerraty 	close(fd);
68*66655411SSimon J. Gerraty 	if (ucp != NULL) {
69*66655411SSimon J. Gerraty 		if (len != NULL)
70f9510887SSimon J. Gerraty 			*len = st.st_size;
71*66655411SSimon J. Gerraty 	}
72*66655411SSimon J. Gerraty #ifdef _STANDALONE
73*66655411SSimon J. Gerraty 	else
74*66655411SSimon J. Gerraty 		printf("%s: out of memory! %lu\n", __func__,
75*66655411SSimon J. Gerraty 		    (unsigned long)len);
76*66655411SSimon J. Gerraty #endif
77*66655411SSimon J. Gerraty 
78f9510887SSimon J. Gerraty 	return (ucp);
79f9510887SSimon J. Gerraty }
80f9510887SSimon J. Gerraty 
81