blob: 60deab7a4ebc50830d31edda8eacafda77fdfaae [file] [log] [blame]
Karl Zhang3de5ab12021-05-31 11:45:48 +08001/*
2 * Copyright (c) 2019-2020, Arm Limited. All rights reserved.
3 *
4 * SPDX-License-Identifier: BSD-3-Clause
5 *
6 */
7
8#include "class_forwards.hpp"
9
10#include "boilerplate.hpp"
11#include "gibberish.hpp"
12#include "compute.hpp"
13#include "data_blocks.hpp"
14#include "psa_asset.hpp"
15#include "find_or_create_asset.hpp"
16#include "template_line.hpp"
17#include "tf_fuzz.hpp"
18#include "crypto_asset.hpp"
19#include "psa_call.hpp"
20
21/**********************************************************************************
22 Methods of class boilerplate follow:
23**********************************************************************************/
24
25void env_var_usage (void)
26{
27 cerr << "Example, for typical TF-M usage, using bash syntax:" << endl
28 << " export TF_FUZZ_LIB_DIR=<path to TF-M installation>/tools/tf_fuzz/lib"
29 << endl
30 << " export TF_FUZZ_BPLATE=tfm_boilerplate.txt" << endl;
31}
32
33void boilerplate::stuff_boilerplate_strings (void)
34{
35 ifstream boilerplate_lib;
36 string file_name; // boilerplate file name
37 string holder;
38 // temp string, e.g., holder for strings read from the boilerplate file
39
40 /* User must point the environment variable $TF_FUZZ_BPLATE to the boilerplate
41 file s/he wants to use, within a directory named by $TF_FUZZ_LIB_DIR. */
42 string bpStringL, bpStringF;
43 char *bpLibDir = getenv ("TF_FUZZ_LIB_DIR");
44 if (bpLibDir == NULL) {
45 cerr << "Error: Please point environment variable $TF_FUZZ_LIB_DIR to "
46 << "TF-Fuzz's library directory." << endl;
47 env_var_usage();
48 exit (701);
49 }
50 bpStringL = bpLibDir;
51 char *bpFName = getenv ("TF_FUZZ_BPLATE");
52 if (bpFName == NULL) {
53 cerr << "Error: Please set environment variable $TF_FUZZ_BPLATE to name the "
54 << "\"boilerplate\" text-library file." << endl;
55 env_var_usage();
56 exit (702);
57 }
58 bpStringF = bpFName;
59 file_name.assign (bpStringL + "/" + bpStringF);
60 boilerplate_lib.open (file_name);
61 if (!boilerplate_lib.is_open()) {
62 cerr << "\nError: Unable to open boilerplate text-library file, at path "
63 << file_name << "." << endl;
64 env_var_usage();
65 exit(200);
66 }
67
68 // Read the strings into the boilerplate vector:
69 getline (boilerplate_lib, holder, '`');
70 /* This first one is "not real." It's a README, in essence, explaining the
71 library-text file, so skip past it. */
72 for (int i = preamble_A; i < n_boilerplate_texts; i++) {
73 if (!getline (boilerplate_lib, holder, '`')) {
74 cerr << "\nError: Unable to read from boilerplate text-library file, at path "
75 << file_name << "." << endl;
76 cerr << " Please make sure the file is not empty." << endl;
77 env_var_usage();
78 exit(851);
79 }
80 if (holder.length() < 5) {
81 cerr << "\nError: Read from boilerplate text-library file, at path "
82 << file_name << ", was too short." << endl;
83 cerr << " Please make sure the file has not been damaged "
84 << "from the TF-Fuzz distribution." << endl;
85 env_var_usage();
86 exit(852);
87 }
88 // Shave off the three-character tag + \n from the front of the string:
89 holder.erase(0, 4);
90 bplate_string.push_back(holder);
91 }
92 boilerplate_lib.close();
93}
94
95boilerplate::boilerplate (void) {
96 stuff_boilerplate_strings();
97}
98
99/**********************************************************************************
100 End of methods of class boilerplate.
101**********************************************************************************/