1 /* Copyright 2013-2014 IBM Corp.
2  *
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  *	http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
12  * implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #include <skiboot.h>
18 #include <opal.h>
19 #include <mem_region.h>
20 #include <lock.h>
21 
22 static struct mem_region *nvram_region;
23 static struct lock fake_nvram_lock = LOCK_UNLOCKED;
24 
fake_nvram_info(uint32_t * total_size)25 int fake_nvram_info(uint32_t *total_size)
26 {
27 	nvram_region = find_mem_region("ibm,fake-nvram");
28 
29 	if (!nvram_region)
30 		return OPAL_HARDWARE;
31 
32 	*total_size = nvram_region->len;
33 
34 	return OPAL_SUCCESS;
35 }
36 
fake_nvram_start_read(void * dst,uint32_t src,uint32_t len)37 int fake_nvram_start_read(void *dst, uint32_t src, uint32_t len)
38 {
39 	if (!nvram_region)
40 		return -ENODEV;
41 
42 	lock(&fake_nvram_lock);
43 	memcpy(dst, (void *) (nvram_region->start + src), len);
44 	unlock(&fake_nvram_lock);
45 
46 	nvram_read_complete(true);
47 
48 	return 0;
49 }
50 
fake_nvram_write(uint32_t offset,void * src,uint32_t size)51 int fake_nvram_write(uint32_t offset, void *src, uint32_t size)
52 {
53 	if (!nvram_region)
54 		return OPAL_HARDWARE;
55 
56 	lock(&fake_nvram_lock);
57 	memcpy((void *) (nvram_region->start + offset), src, size);
58 	unlock(&fake_nvram_lock);
59 
60 	return 0;
61 }
62 
63