xref: /openbmc/hiomapd/test/system.c (revision f1e547c7)
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 = {
15 };
16 
17 static struct mtd_info_user mtd = {
18 	.type = MTD_NORFLASH,
19 	.flags = MTD_WRITEABLE,
20 };
21 
22 void system_set_reserved_size(uint32_t size)
23 {
24 	ctrl.size = size;
25 }
26 
27 void system_set_mtd_sizes(uint32_t size, uint32_t erasesize)
28 {
29 	mtd.size = size;
30 	mtd.erasesize = erasesize;
31 	mtd.writesize = erasesize;
32 }
33 
34 int ioctl(int fd, unsigned long request, ...)
35 {
36 	int rc = 0;
37 	va_list ap;
38 
39 	switch (request) {
40 	case MEMGETINFO:
41 	{
42 		struct mtd_info_user *info;
43 
44 		va_start(ap, request);
45 		info = va_arg(ap, struct mtd_info_user *);
46 		memcpy(info, &mtd, sizeof(mtd));
47 		va_end(ap);
48 		break;
49 	}
50 	case MEMERASE:
51 	{
52 		struct erase_info_user *info;
53 		uint8_t *map;
54 
55 		va_start(ap, request);
56 		info = va_arg(ap, struct erase_info_user *);
57 
58 		if (info->start + info->length > mtd.size)
59 			return -1;
60 
61 		map = mmap(NULL, mtd.size, PROT_WRITE, MAP_SHARED, fd, 0);
62 		if (map == MAP_FAILED)
63 			return -1;
64 
65 		memset(&map[info->start], 0xff, info->length);
66 		munmap(map, mtd.size);
67 
68 		va_end(ap);
69 		break;
70 	}
71 	case ASPEED_LPC_CTRL_IOCTL_GET_SIZE:
72 	{
73 		struct aspeed_lpc_ctrl_mapping *info;
74 
75 		va_start(ap, request);
76 		info = va_arg(ap, struct aspeed_lpc_ctrl_mapping *);
77 		info->size = ctrl.size;
78 		va_end(ap);
79 		break;
80 	}
81 	case ASPEED_LPC_CTRL_IOCTL_MAP:
82 		break;
83 	default:
84 		printf("ioctl() called with unhandled request 0x%08lx\n", request);
85 		rc = -1;
86 		break;
87 	}
88 
89 	return rc;
90 }
91