Fix all compilation warnings
[fw/stlink] / src / st-term.c
index 7f045b6e4f05d8eb6cda811913ba19b971784e4d..bbf7ea37ef714a7e39062c5ee6ee5f173d44e0b0 100644 (file)
@@ -1,4 +1,5 @@
 #include <stdio.h>
+#include <stdlib.h>
 /* According to POSIX.1-2001 */
 #include <sys/select.h>
 #include <string.h>
 
 #define STLINKY_MAGIC 0xDEADF00D
 
+#define READ_UINT32_LE(buf)  ((uint32_t) (   buf[0]         \
+                                           | buf[1] <<  8   \
+                                           | buf[2] << 16   \
+                                           | buf[3] << 24))
+
 struct stlinky {
        stlink_t *sl;
        uint32_t off;
@@ -25,17 +31,17 @@ struct stlinky*  stlinky_detect(stlink_t* sl)
        static const uint32_t sram_base = 0x20000000;
        struct stlinky* st = malloc(sizeof(struct stlinky));
        st->sl = sl;
-       printf("sram: 0x%x bytes @ 0x%x\n", sl->sram_base, sl->sram_size);
+       printf("sram: 0x%x bytes @ 0x%zx\n", sl->sram_base, sl->sram_size);
        uint32_t off;
        for (off = 0; off < sl->sram_size; off += 4) {
                stlink_read_mem32(sl, sram_base + off, 4);
-               if ( STLINKY_MAGIC== *(uint32_t*) sl->q_buf)
+               if (STLINKY_MAGIC == READ_UINT32_LE(sl->q_buf))
                {
                        printf("stlinky detected at 0x%x\n", sram_base + off);
                        st->off = sram_base + off;
                        stlink_read_mem32(sl, st->off + 4, 4);
                        st->bufsize = (size_t) *(unsigned char*) sl->q_buf;
-                       printf("stlinky buffer size 0x%zu\n", st->bufsize);
+                       printf("stlinky buffer size 0x%zu \n", st->bufsize);
                        return st;
                }
        }
@@ -117,10 +123,17 @@ void nonblock(int state)
 }
 
 static int keep_running = 1;
+static int sigcount=0;
 void cleanup(int dummy)
 {
+       (void) dummy;
+       sigcount++;
        keep_running = 0;
-       printf("\n\nGot a signal - terminating\n");
+       printf("\n\nGot a signal\n");
+       if (sigcount==2) {
+               printf("\n\nGot a second signal - bailing out\n");
+               exit(1);
+       }
 }
 
 
@@ -130,7 +143,7 @@ int main(int ac, char** av) {
        /* unused */
        ac = ac;
        av = av;
-       sl = stlink_open_usb(10);
+       sl = stlink_open_usb(10, 1);
        if (sl != NULL) {
                printf("ST-Linky proof-of-concept terminal :: Created by Necromant for lulz\n");
                stlink_version(sl);
@@ -155,7 +168,8 @@ int main(int ac, char** av) {
                struct stlinky *st = stlinky_detect(sl);
                if (st == NULL)
                {
-                       printf("stlinky magic not found in sram :(");
+                       printf("stlinky magic not found in sram :(\n");
+                       goto bailout;
                }
                char* rxbuf = malloc(st->bufsize);
                char* txbuf = malloc(st->bufsize);
@@ -165,7 +179,7 @@ int main(int ac, char** av) {
                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");
+               printf("Entering interactive terminal. CTRL+C to exit\n\n\n");
                while(1) {
                        if (stlinky_canrx(st)) {
                                tmp = stlinky_rx(st, rxbuf);
@@ -179,6 +193,7 @@ int main(int ac, char** av) {
                        if (!keep_running)
                                break;
                }
+       bailout:
                nonblock(0);
                stlink_exit_debug_mode(sl);
                stlink_close(sl);