3 #include "core/command.hpp"
4 #include "core/controller.hpp"
5 #include "core/memorymanip.hpp"
6 #include "core/misc.hpp"
7 #include "core/movie.hpp"
8 #include "core/rom.hpp"
9 #include "core/moviedata.hpp"
10 #include "core/settings.hpp"
11 #include "core/window.hpp"
12 #include "library/crandom.hpp"
13 #include "library/directory.hpp"
14 #include "library/hex.hpp"
15 #include "library/loadlib.hpp"
16 #include "library/sha256.hpp"
17 #include "library/string.hpp"
18 #include "library/skein.hpp"
19 #include "library/serialization.hpp"
20 #include "library/arch-detect.hpp"
36 #include <boost/filesystem.hpp>
37 #if defined(_WIN32) || defined(_WIN64)
41 #ifdef USE_LIBGCRYPT_SHA256
48 bool reached_main_flag
;
49 threads::lock seed_mutex
;
51 uint64_t arch_get_tsc()
55 asm volatile("rdtsc" : "=a"(a
), "=d"(b
));
56 return ((uint64_t)b
<< 32) | a
;
62 uint64_t arch_get_random()
66 //This reads undefined value if RDRAND is not supported. Don't care.
67 asm volatile (".byte 0xb8, 0x01, 0x00, 0x00, 0x00, 0x0f, 0xa2, 0xf7, 0xc1, 0x00, 0x00, 0x00, 0x40, "
68 "0x74, 0x03, 0x0f, 0xc7, 0xf0" : "=a"(r
) : : "ebx", "ecx", "edx");
77 const unsigned slots
= 32;
78 static unsigned count
= 0;
79 static uint64_t last_reseed
= 0;
80 static uint64_t buf
[slots
+ 1];
81 buf
[count
++] = arch_get_tsc();
82 threads::alock
h(seed_mutex
);
83 if(count
== 0) count
= 1; //Shouldn't happen.
84 if(count
== slots
|| buf
[count
- 1] - last_reseed
> 300000000) {
85 last_reseed
= buf
[count
- 1];
86 buf
[slots
] = arch_get_random();
87 prng
.write(buf
, sizeof(buf
));
92 std::string
get_random_hexstring_64(size_t index
)
94 threads::alock
h(seed_mutex
);
98 buf
[3] = arch_get_random();
99 buf
[4] = arch_get_random();
100 buf
[5] = arch_get_random();
101 buf
[6] = arch_get_random();
102 gettimeofday(&tv
, NULL
);
105 buf
[2] = arch_get_tsc();
106 prng
.write(buf
, sizeof(buf
));
107 prng
.read(out
, sizeof(out
));
108 return hex::b_to(out
, sizeof(out
));
111 char endian_char(int e
)
120 void fatal_signal_handler(int sig
)
122 write(2, "Caught fatal signal!\n", 21);
123 if(movb
) emerg_save_movie(movb
.get_mfile(), movb
.get_rrdata());
124 signal(sig
, SIG_DFL
);
128 void terminate_handler()
130 write(2, "Terminating abnormally!\n", 24);
131 if(movb
) emerg_save_movie(movb
.get_mfile(), movb
.get_rrdata());
132 std::cerr
<< "Exiting on fatal error" << std::endl
;
136 command::fnptr
<const std::string
&> test4(lsnes_cmd
, "panicsave-movie", "", "",
137 [](const std::string
& args
) throw(std::bad_alloc
, std::runtime_error
) {
138 if(movb
) emerg_save_movie(movb
.get_mfile(), movb
.get_rrdata());
141 //% is intentionally missing.
142 const char* allowed_filename_chars
= "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789"
143 "^&'@{}[],$?!-#().+~_";
146 std::string
safe_filename(const std::string
& str
)
148 std::ostringstream o
;
149 for(size_t i
= 0; i
< str
.length(); i
++) {
150 unsigned char ch
= static_cast<unsigned char>(str
[i
]);
151 if(strchr(allowed_filename_chars
, ch
))
154 o
<< "%" << hex::to8(ch
);
159 std::string
get_random_hexstring(size_t length
) throw(std::bad_alloc
)
162 for(size_t i
= 0; i
< length
; i
+= 64)
163 out
= out
+ get_random_hexstring_64(i
);
164 return out
.substr(0, length
);
167 void set_random_seed(const std::string
& seed
) throw(std::bad_alloc
)
169 std::vector
<char> x(seed
.begin(), seed
.end());
171 threads::alock
h(seed_mutex
);
172 prng
.write(&x
[0], x
.size());
176 void set_random_seed() throw(std::bad_alloc
)
179 crandom::generate(buf
, 128);
180 std::string
s(buf
, sizeof(buf
));
184 struct loaded_rom
load_rom_from_commandline(std::vector
<std::string
> cmdline
) throw(std::bad_alloc
,
189 for(auto i
: cmdline
) {
190 if(!(optp
= regex("--rom=(.+)", i
)))
198 } catch(std::bad_alloc
& e
) {
200 } catch(std::exception
& e
) {
201 throw std::runtime_error(std::string("Can't load ROM: ") + e
.what());
204 std::string not_present
= "N/A";
205 for(size_t i
= 0; i
< ROM_SLOT_COUNT
; i
++) {
206 std::string romname
= "UNKNOWN ROM";
207 std::string xmlname
= "UNKNOWN XML";
208 if(i
< r
.rtype
->get_image_count()) {
209 romname
= (r
.rtype
->get_image_info(i
).hname
== "ROM") ? std::string("ROM") :
210 (r
.rtype
->get_image_info(i
).hname
+ " ROM");
211 xmlname
= r
.rtype
->get_image_info(i
).hname
+ " XML";
213 if(r
.romimg
[i
].data
) messages
<< romname
<< " hash: " << r
.romimg
[i
].sha_256
.read() << std::endl
;
214 if(r
.romxml
[i
].data
) messages
<< xmlname
<< " hash: " << r
.romxml
[i
].sha_256
.read() << std::endl
;
219 void dump_region_map() throw(std::bad_alloc
)
221 std::list
<struct memory_region
*> regions
= lsnes_memory
.get_regions();
222 for(auto i
: regions
) {
223 std::ostringstream x
;
224 x
<< hex::to(i
->base
) << "-" << hex::to(i
->last_address()) << " " << hex::to(i
->size
) << " ";
225 messages
<< x
.str() << (i
->readonly
? "R-" : "RW") << endian_char(i
->endian
)
226 << (i
->special
? 'I' : 'M') << " " << i
->name
<< std::endl
;
230 void fatal_error() throw()
232 platform::fatal_error();
233 std::cout
<< "PANIC: Fatal error, can't continue." << std::endl
;
237 std::string
get_config_path() throw(std::bad_alloc
)
241 if((tmp
= getenv("APPDATA"))) {
242 //If $APPDATA exists, it is the base directory
244 } else if((tmp
= getenv("XDG_CONFIG_HOME"))) {
245 //If $XDG_CONFIG_HOME exists, it is the base directory
247 } else if((tmp
= getenv("HOME"))) {
248 //If $HOME exists, the base directory is '.config' there.
249 basedir
= std::string(tmp
) + "/.config";
251 //Last chance: Return current directory.
254 //Try to create 'lsnes'. If it exists (or is created) and is directory, great. Otherwise error out.
255 std::string lsnes_path
= basedir
+ "/lsnes";
256 if(!ensure_directory_exists(lsnes_path
)) {
257 messages
<< "FATAL: Can't create configuration directory '" << lsnes_path
<< "'" << std::endl
;
260 //Yes, this is racy, but portability is more important than being absolutely correct...
261 std::string tfile
= lsnes_path
+ "/test";
262 remove(tfile
.c_str());
264 if(!(x
= fopen(tfile
.c_str(), "w+"))) {
265 messages
<< "FATAL: Configuration directory '" << lsnes_path
<< "' is not writable" << std::endl
;
269 remove(tfile
.c_str());
275 if(movb
) emerg_save_movie(movb
.get_mfile(), movb
.get_rrdata());
276 messages
<< "FATAL: Out of memory!" << std::endl
;
280 std::ostream
& messages_relay_class::getstream() { return platform::out(); }
281 messages_relay_class messages
;
283 uint32_t gcd(uint32_t a
, uint32_t b
) throw()
288 return gcd(b
, a
% b
);
291 std::string
format_address(void* addr
)
293 return hex::to((uint64_t)addr
);
296 bool in_global_ctors()
298 return !reached_main_flag
;
304 new_core_flag
= false; //We'll process the static cores anyway.
305 reached_main_flag
= true;
306 lsnes_cmd
.set_oom_panic(OOM_panic
);
307 lsnes_cmd
.set_output(platform::out());
308 loadlib::module::run_initializers();
309 std::set_terminate(terminate_handler
);
311 signal(SIGHUP
, fatal_signal_handler
);
314 signal(SIGINT
, fatal_signal_handler
);
317 signal(SIGQUIT
, fatal_signal_handler
);
320 signal(SIGILL
, fatal_signal_handler
);
323 signal(SIGABRT
, fatal_signal_handler
);
326 signal(SIGSEGV
, fatal_signal_handler
);
329 signal(SIGFPE
, fatal_signal_handler
);
332 signal(SIGPIPE
, fatal_signal_handler
);
335 signal(SIGBUS
, fatal_signal_handler
);
338 signal(SIGTRAP
, fatal_signal_handler
);
341 signal(SIGTERM
, fatal_signal_handler
);
345 std::string
mangle_name(const std::string
& orig
)
347 std::ostringstream out
;
354 out
<< "\xE2\x8F\xBF";
356 out
<< "\xE2\x8B\xBF";
363 void random_mix_timing_entropy()
368 //Generate highly random stuff.
369 void highrandom_256(uint8_t* buf
)
372 std::string s
= get_random_hexstring(64);
373 std::copy(s
.begin(), s
.end(), reinterpret_cast<char*>(tmp
));
374 crandom::generate(tmp
+ 64, 32);
375 serialization::u64b(tmp
+ 96, arch_get_tsc());
376 skein::hash
hsh(skein::hash::PIPE_1024
, 256);
381 std::string
get_temp_file()
383 #if !defined(_WIN32) && !defined(_WIN64)
385 strcpy(tname
, "/tmp/lsnestmp_XXXXXX");
386 int h
= mkstemp(tname
);
388 throw std::runtime_error("Failed to get new tempfile name");
394 if(!GetTempPathA(512, tpath
))
395 throw std::runtime_error("Failed to get new tempfile name");
396 if(!GetTempFileNameA(tpath
, "lsn", 0, tname
))
397 throw std::runtime_error("Failed to get new tempfile name");
402 command::fnptr
<const std::string
&> macro_test(lsnes_cmd
, "test-macro", "", "",
403 [](const std::string
& args
) throw(std::bad_alloc
, std::runtime_error
) {
404 regex_results r
= regex("([0-9]+)[ \t](.*)", args
);
406 messages
<< "Bad syntax" << std::endl
;
409 unsigned ctrl
= parse_value
<unsigned>(r
[1]);
410 auto pcid
= controls
.lcid_to_pcid(ctrl
);
412 messages
<< "Bad controller" << std::endl
;
416 const port_controller
* _ctrl
= controls
.get_blank().porttypes().port_type(pcid
.first
).
417 controller_info
->get(pcid
.second
);
419 messages
<< "No controller data for controller" << std::endl
;
422 controller_macro_data
mdata(r
[2].c_str(), controller_macro_data::make_descriptor(*_ctrl
), 0);
423 messages
<< "Macro: " << mdata
.dump(*_ctrl
) << std::endl
;
424 } catch(std::exception
& e
) {
425 messages
<< "Exception: " << e
.what() << std::endl
;