xref: /openbmc/hiomapd/test/system.c (revision 68a24c9ea5ce11c87fab22a3f4648c7d88c98fee)
1 // SPDX-License-Identifier: Apache-2.0
2 // Copyright (C) 2018 IBM Corp.
3 
4 #include <linux/types.h>
5 #include <mtd/mtd-user.h>
6 #include <stdarg.h>
7 #include <stdint.h>
8 #include <stdio.h>
9 #include <string.h>
10 #include <sys/mman.h>
11 
12 #include "linux/aspeed-lpc-ctrl.h"
13 
14 static struct aspeed_lpc_ctrl_mapping ctrl = { .size = 0 };
15 
16 static struct mtd_info_user mtd = {
17 	.type = MTD_NORFLASH,
18 	.flags = MTD_WRITEABLE,
19 };
20 
system_set_reserved_size(uint32_t size)21 void system_set_reserved_size(uint32_t size)
22 {
23 	ctrl.size = size;
24 }
25 
system_set_mtd_sizes(uint32_t size,uint32_t erasesize)26 void system_set_mtd_sizes(uint32_t size, uint32_t erasesize)
27 {
28 	mtd.size = size;
29 	mtd.erasesize = erasesize;
30 	mtd.writesize = erasesize;
31 }
32 
ioctl(int fd,unsigned long request,...)33 int ioctl(int fd, unsigned long request, ...)
34 {
35 	int rc = 0;
36 	va_list ap;
37 
38 	switch (request) {
39 	case MEMGETINFO:
40 	{
41 		struct mtd_info_user *info;
42 
43 		va_start(ap, request);
44 		info = va_arg(ap, struct mtd_info_user *);
45 		memcpy(info, &mtd, sizeof(mtd));
46 		va_end(ap);
47 		break;
48 	}
49 	case MEMERASE:
50 	{
51 		struct erase_info_user *info;
52 		uint8_t *map;
53 
54 		va_start(ap, request);
55 		info = va_arg(ap, struct erase_info_user *);
56 
57 		if (info->start + info->length > mtd.size)
58 			return -1;
59 
60 		map = mmap(NULL, mtd.size, PROT_WRITE, MAP_SHARED, fd, 0);
61 		if (map == MAP_FAILED)
62 			return -1;
63 
64 		memset(&map[info->start], 0xff, info->length);
65 		munmap(map, mtd.size);
66 
67 		va_end(ap);
68 		break;
69 	}
70 	case ASPEED_LPC_CTRL_IOCTL_GET_SIZE:
71 	{
72 		struct aspeed_lpc_ctrl_mapping *info;
73 
74 		va_start(ap, request);
75 		info = va_arg(ap, struct aspeed_lpc_ctrl_mapping *);
76 		info->size = ctrl.size;
77 		va_end(ap);
78 		break;
79 	}
80 	case ASPEED_LPC_CTRL_IOCTL_MAP:
81 		break;
82 	default:
83 		printf("ioctl() called with unhandled request 0x%08lx\n", request);
84 		rc = -1;
85 		break;
86 	}
87 
88 	return rc;
89 }
90