aboutsummaryrefslogtreecommitdiffstats
path: root/mpm/tools/tlv_eeprom/tlv_eeprom_io.c
blob: 83586e4f6b02872139cd20a27e30702f084da268 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
// SPDX-License-Identifier: GPL-2.0-or-later
/*
 * Copyright 2019 Ettus Research, a National Instruments Brand
 */

#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <unistd.h>

#include "tlv_eeprom.h"

struct tlv_eeprom *tlv_eeprom_read_from_file(const char *path)
{
	struct tlv_eeprom *ep;
	int fd;
	uint8_t *ptr;
	size_t len;
	ssize_t rd;

	fd = open(path, O_RDONLY);
	if (fd < 0) {
		perror("couldn't open file");
		return NULL;
	}


	ep = malloc(sizeof(struct tlv_eeprom));
	if (!ep) {
		perror("alloc failed");
		return NULL;
	}

	len = sizeof(struct tlv_eeprom);
	ptr = (uint8_t *)ep;

	while (len) {
		rd = read(fd, ptr, len);
		if (rd < 0) {
			perror("read failed");
			goto out;
		}

		len -= rd;
		ptr += rd;

		if (!rd)
			break;
	}

	return ep;
out:
	free(ep);
	return NULL;
}

int tlv_eeprom_write_to_file(const struct tlv_eeprom *e, const char *path)
{
	int fd, rv = 0;
	uint8_t *ptr;
	size_t len;
	ssize_t wr;

	fd = open(path, O_WRONLY | O_CREAT, 0666);
	if (fd < 0) {
		perror("couldn't open file");
		return -1;
	}

	len = sizeof(*e);
	ptr = (uint8_t *)e;

	while (len) {
		wr = write(fd, ptr, len);
		if (wr < 0) {
			perror("error writing file");
			goto out;
		}
		len -= wr;
		ptr += wr;
	}

out:
	close(fd);
	return rv;
}