- stlink_t* sl;
-
- /* unused */
- ac = ac;
- av = av;
- sl = stlink_open_usb(10);
- if (sl != NULL) {
- printf("ST-Linky proof-of-concept terminal :: Created by Necromant for lulz\n");
- stlink_version(sl);
- stlink_enter_swd_mode(sl);
- printf("chip id: %#x\n", sl->chip_id);
- printf("core_id: %#x\n", sl->core_id);
-
- cortex_m3_cpuid_t cpuid;
- stlink_cpu_id(sl, &cpuid);
- printf("cpuid:impl_id = %0#x, variant = %#x\n", cpuid.implementer_id, cpuid.variant);
- printf("cpuid:part = %#x, rev = %#x\n", cpuid.part, cpuid.revision);
-
- stlink_reset(sl);
- stlink_force_debug(sl);
- stlink_run(sl);
- stlink_status(sl);
-
- /* wait for device to boot */
- /* TODO: Make timeout adjustable via command line */
- sleep(1);
-
- struct stlinky *st = stlinky_detect(sl);
- if (st == NULL)
- {
- printf("stlinky magic not found in sram :(\n");
- goto bailout;
- }
- char* rxbuf = malloc(st->bufsize);
- char* txbuf = malloc(st->bufsize);
- size_t tmp;
- nonblock(1);
- int fd = fileno(stdin);
- int saved_flags = fcntl(fd, F_GETFL);
- fcntl(fd, F_SETFL, saved_flags & ~O_NONBLOCK);
- signal(SIGINT, cleanup);
- printf("Entering interactive terminal. CTRL+C to exit\n\n\n");
- while(1) {
- if (stlinky_canrx(st)) {
- tmp = stlinky_rx(st, rxbuf);
- fwrite(rxbuf,tmp,1,stdout);
- fflush(stdout);
- }
- if (kbhit()) {
- tmp = read(fd, txbuf, st->bufsize);
- stlinky_tx(st,txbuf,tmp);
- }
- if (!keep_running)
- break;
- }
- bailout:
- nonblock(0);
- stlink_exit_debug_mode(sl);
- stlink_close(sl);
- }
- return 0;
+ struct stlinky *st=NULL;
+
+ sig_init();
+
+ sl = stlink_open_usb(10, 1, NULL);
+ if (sl != NULL) {
+ printf("ST-Linky proof-of-concept terminal :: Created by Necromant for lulz\n");
+ stlink_version(sl);
+ stlink_enter_swd_mode(sl);
+ printf("chip id: %#x\n", sl->chip_id);
+ printf("core_id: %#x\n", sl->core_id);
+
+ cortex_m3_cpuid_t cpuid;
+ stlink_cpu_id(sl, &cpuid);
+ printf("cpuid:impl_id = %0#x, variant = %#x\n", cpuid.implementer_id, cpuid.variant);
+ printf("cpuid:part = %#x, rev = %#x\n", cpuid.part, cpuid.revision);
+
+ stlink_reset(sl);
+ stlink_force_debug(sl);
+ stlink_run(sl);
+ stlink_status(sl);
+
+ /* wait for device to boot */
+ /* TODO: Make timeout adjustable via command line */
+ sleep(1);
+
+ if(ac == 1){
+ st = stlinky_detect(sl);
+ }else if(ac == 2){
+ st = malloc(sizeof(struct stlinky));
+ st->sl = sl;
+ st->off = (int)strtol(av[1], NULL, 16);
+ printf("using stlinky at 0x%x\n", st->off);
+ stlink_read_mem32(sl, st->off + 4, 4);
+ st->bufsize = READ_UINT32_LE(sl->q_buf);
+ printf("stlinky buffer size 0x%zu \n", st->bufsize);
+ }else{
+ cleanup(0);
+ }
+ if (st == NULL)
+ {
+ printf("stlinky magic not found in sram :(\n");
+ cleanup(0);
+ }
+ if (st->bufsize > ST_TERM_MAX_BUFF_SIZE){
+ printf("stlinky buffer size too big\n");
+ cleanup(0);
+ }
+ char* rxbuf = malloc(st->bufsize);
+ char* txbuf = malloc(st->bufsize);
+ size_t tmp;
+ nonblock(1);
+ int fd = fileno(stdin);
+ int saved_flags = fcntl(fd, F_GETFL);
+ fcntl(fd, F_SETFL, saved_flags & ~O_NONBLOCK);
+ printf("Entering interactive terminal. CTRL+C to exit\n\n\n");
+ while(1) {
+ sig_process();
+ tmp = stlinky_rx(st, rxbuf);
+ if(tmp > 0)
+ {
+ fwrite(rxbuf,tmp,1,stdout);
+ fflush(stdout);
+ }
+ if (kbhit()) {
+ tmp = read(fd, txbuf, st->bufsize);
+ stlinky_tx(st,txbuf,tmp);
+ }
+ }
+ }
+ return 0;