xref: /openbmc/hiomapd/test/flash_copy.c (revision 0297e5b82ea1bf8b151f8208c5f28a121d82eb4c)
1e0cdd3eaSAndrew Jeffery // SPDX-License-Identifier: Apache-2.0
2e0cdd3eaSAndrew Jeffery // Copyright (C) 2018 IBM Corp.
3e0cdd3eaSAndrew Jeffery 
4e0cdd3eaSAndrew Jeffery #include <assert.h>
5e0cdd3eaSAndrew Jeffery #include <fcntl.h>
6e0cdd3eaSAndrew Jeffery #include <stdlib.h>
7e0cdd3eaSAndrew Jeffery #include <string.h>
8e0cdd3eaSAndrew Jeffery #include <sys/types.h>
9e0cdd3eaSAndrew Jeffery #include <sys/stat.h>
10e0cdd3eaSAndrew Jeffery #include <unistd.h>
11e0cdd3eaSAndrew Jeffery 
12e0cdd3eaSAndrew Jeffery #include "common.h"
1326558dbbSAndrew Jeffery #include "mboxd.h"
14f1e547c7SEvan Lojewski #include "backend.h"
15e0cdd3eaSAndrew Jeffery 
162dfc2a22SAndrew Jeffery #include "test/system.h"
17e0cdd3eaSAndrew Jeffery #include "test/tmpf.h"
18e0cdd3eaSAndrew Jeffery 
19e0cdd3eaSAndrew Jeffery #define TEST_SIZE 4096
20e0cdd3eaSAndrew Jeffery 
21e0cdd3eaSAndrew Jeffery static struct tmpf tmp;
22e0cdd3eaSAndrew Jeffery 
cleanup(void)23e0cdd3eaSAndrew Jeffery void cleanup(void)
24e0cdd3eaSAndrew Jeffery {
25e0cdd3eaSAndrew Jeffery 	tmpf_destroy(&tmp);
26e0cdd3eaSAndrew Jeffery }
27e0cdd3eaSAndrew Jeffery 
main(void)28e0cdd3eaSAndrew Jeffery int main(void)
29e0cdd3eaSAndrew Jeffery {
302dfc2a22SAndrew Jeffery 	struct mbox_context context = {0};
31e0cdd3eaSAndrew Jeffery 	ssize_t processed;
32e0cdd3eaSAndrew Jeffery 	int rand_fd;
33e0cdd3eaSAndrew Jeffery 	char *src;
34e0cdd3eaSAndrew Jeffery 	char *dst;
35e0cdd3eaSAndrew Jeffery 	int rc;
36e0cdd3eaSAndrew Jeffery 
37e0cdd3eaSAndrew Jeffery 	atexit(cleanup);
38e0cdd3eaSAndrew Jeffery 
392dfc2a22SAndrew Jeffery 	system_set_mtd_sizes(TEST_SIZE, TEST_SIZE);
402dfc2a22SAndrew Jeffery 
41e0cdd3eaSAndrew Jeffery 	mbox_vlog = &mbox_log_console;
42e0cdd3eaSAndrew Jeffery 
43e0cdd3eaSAndrew Jeffery 	src = malloc(TEST_SIZE);
44e0cdd3eaSAndrew Jeffery 	dst = malloc(TEST_SIZE);
45e0cdd3eaSAndrew Jeffery 	if (!(src && dst)) {
46e0cdd3eaSAndrew Jeffery 		rc = -1;
47e0cdd3eaSAndrew Jeffery 		goto free;
48e0cdd3eaSAndrew Jeffery 	}
49e0cdd3eaSAndrew Jeffery 
50e0cdd3eaSAndrew Jeffery 	rand_fd = open("/dev/urandom", O_RDONLY);
51e0cdd3eaSAndrew Jeffery 	if (rand_fd < 0) {
52e0cdd3eaSAndrew Jeffery 		rc = rand_fd;
53e0cdd3eaSAndrew Jeffery 		goto free;
54e0cdd3eaSAndrew Jeffery 	}
55e0cdd3eaSAndrew Jeffery 
56e0cdd3eaSAndrew Jeffery 	rc = tmpf_init(&tmp, "flash-store.XXXXXX");
57e0cdd3eaSAndrew Jeffery 	if (rc < 0)
58e0cdd3eaSAndrew Jeffery 		goto free;
59e0cdd3eaSAndrew Jeffery 
60e0cdd3eaSAndrew Jeffery 	processed = read(rand_fd, src, TEST_SIZE);
61e0cdd3eaSAndrew Jeffery 	if (processed != TEST_SIZE) {
62e0cdd3eaSAndrew Jeffery 		rc = -1;
63e0cdd3eaSAndrew Jeffery 		goto free;
64e0cdd3eaSAndrew Jeffery 	}
65e0cdd3eaSAndrew Jeffery 
66e0cdd3eaSAndrew Jeffery 	processed = write(tmp.fd, src, TEST_SIZE);
67e0cdd3eaSAndrew Jeffery 	if (processed != TEST_SIZE) {
68e0cdd3eaSAndrew Jeffery 		rc = -1;
69e0cdd3eaSAndrew Jeffery 		goto free;
70e0cdd3eaSAndrew Jeffery 	}
71e0cdd3eaSAndrew Jeffery 
72f1e547c7SEvan Lojewski 	assert(!backend_probe_mtd(&context.backend, tmp.path));
73e0cdd3eaSAndrew Jeffery 
74*0297e5b8SAndrew Jeffery 	backend_copy(&context.backend, 0, dst, TEST_SIZE);
75e0cdd3eaSAndrew Jeffery 	assert(0 == memcmp(src, dst, TEST_SIZE));
76e0cdd3eaSAndrew Jeffery 
77f1e547c7SEvan Lojewski 	backend_free(&context.backend);
78f1e547c7SEvan Lojewski 
79e0cdd3eaSAndrew Jeffery free:
80f1e547c7SEvan Lojewski 
81e0cdd3eaSAndrew Jeffery 	free(src);
82e0cdd3eaSAndrew Jeffery 	free(dst);
83e0cdd3eaSAndrew Jeffery 
84e0cdd3eaSAndrew Jeffery 	return rc;
85e0cdd3eaSAndrew Jeffery }
86