summaryrefslogtreecommitdiffstats
path: root/src/core/nvs.c
blob: 9b4d1310a9efcf669abb187b5f9766da4be02b14 (plain) (blame)
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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
/*
 * Copyright (C) 2006 Michael Brown <mbrown@fensystems.co.uk>.
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License as
 * published by the Free Software Foundation; either version 2 of the
 * License, or any later version.
 *
 * This program is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 */

#include <stdint.h>
#include <string.h>
#include <gpxe/dhcp.h>
#include <gpxe/nvs.h>

/** @file
 *
 * Non-volatile storage
 *
 */

static size_t nvs_options_len ( struct nvs_device *nvs ) {
	struct dhcp_option *option;
	uint8_t sum;
	unsigned int i;
	size_t len;

	for ( sum = 0, i = 0 ; i < nvs->len ; i++ ) {
		sum += * ( ( uint8_t * ) ( nvs->options->data + i ) );
	}
	if ( sum != 0 ) {
		DBG ( "NVS %p has bad checksum %02x; assuming empty\n",
		      nvs, sum );
		return 0;
	}

	option = nvs->options->data;
	if ( option->tag == DHCP_PAD ) {
		DBG ( "NVS %p has bad start; assuming empty\n", nvs );
		return 0;
	}
	
	option = find_dhcp_option ( nvs->options, DHCP_END );
	if ( ! option ) {
		DBG ( "NVS %p has no end tag; assuming empty\n", nvs );
		return 0;
	}

	len = ( ( void * ) option - nvs->options->data + 1 );
	DBG ( "NVS %p contains %zd bytes of options (maximum %zd)\n",
	      nvs, len, nvs->len );

	return len;
}

int nvs_register ( struct nvs_device *nvs ) {
	struct dhcp_option *option;
	int rc;

	nvs->options = alloc_dhcp_options ( nvs->len );
	if ( ! nvs->options ) {
		DBG ( "NVS %p could not allocate %zd bytes\n", nvs, nvs->len );
		rc = -ENOMEM;
		goto err;
	}

	if ( ( rc = nvs->op->read ( nvs, 0, nvs->options->data,
				    nvs->len ) ) != 0 ) {
		DBG ( "NVS %p could not read [0,%zd)\n", nvs, nvs->len );
		goto err;
	}

	nvs->options->len = nvs->options->max_len;
	nvs->options->len = nvs_options_len ( nvs );
	if ( ! nvs->options->len ) {
		option = nvs->options->data;
		option->tag = DHCP_END;
		nvs->options->len = 1;
	}

	register_dhcp_options ( nvs->options );

	return 0;
	
 err:
	
	free_dhcp_options ( nvs->options );
	nvs->options = NULL;
	return rc;
}

void nvs_unregister ( struct nvs_device *nvs ) {
	if ( nvs->options ) {
		unregister_dhcp_options ( nvs->options );
		free_dhcp_options ( nvs->options );
		nvs->options = NULL;
	}
}