1// Copyright 2016 The Fuchsia Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5#include <zircon/device/tpm.h>
6#include <errno.h>
7#include <fcntl.h>
8#include <zircon/syscalls.h>
9#include <zircon/types.h>
10#include <lib/fdio/util.h>
11#include <stddef.h>
12#include <stdio.h>
13#include <stdlib.h>
14#include <string.h>
15#include <unistd.h>
16
17const char* prog_name;
18
19void print_usage(void) {
20    printf("Usage:\n");
21    printf("\n");
22    printf("%s save\n", prog_name);
23    printf("save: Issue a TPM_SaveState command.\n");
24}
25
26int cmd_save_state(int fd, int argc, const char** argv) {
27    ssize_t ret = ioctl_tpm_save_state(fd);
28    if (ret < 0) {
29        printf("Error when saving state: (%zd)\n", ret);
30        return 1;
31    }
32
33    return 0;
34}
35
36int main(int argc, const char** argv) {
37    if (argc < 1)
38        return 1;
39
40    prog_name = argv[0];
41
42    if (argc < 2) {
43        print_usage();
44        return 1;
45    }
46
47    const char* cmd = argv[1];
48
49    argc -= 2;
50    argv += 2;
51
52    int fd = open("/dev/class/tpm/000", O_RDWR);
53    if (fd < 0) {
54        printf("Error opening TPM device.\n");
55        return 1;
56    }
57
58    if (!strcmp("save", cmd)) {
59        return cmd_save_state(fd, argc, argv);
60    } else {
61        printf("Unrecognized command %s.\n", cmd);
62        print_usage();
63        return 1;
64    }
65
66    printf("We should never get here!.\n");
67    return 1;
68}
69