Fastbootd: General fixes and changes

read data once bug fix
ability to run fastbootd without network and named socket configuration in init.rc
vendortrigger name changed to fastbootd
deleted unused function from default implementation of OEM library

Change-Id: Ib957fae8172530f20d51bb51b5e07bccab07e555
diff --git a/fastbootd/commands.c b/fastbootd/commands.c
index 2f6e86a..5c283a8 100644
--- a/fastbootd/commands.c
+++ b/fastbootd/commands.c
@@ -228,6 +228,7 @@
         return;
     }
 
+    //TODO: add same verification as in cmd_flash
     if (phandle->download_fd < 0) {
         fastboot_fail(phandle, "no layout file");
         return;
diff --git a/fastbootd/commands/flash.h b/fastbootd/commands/flash.h
index 86dc811..5a64cab 100644
--- a/fastbootd/commands/flash.h
+++ b/fastbootd/commands/flash.h
@@ -32,6 +32,11 @@
 #ifndef _FASTBOOTD_ERASE_H
 #define _FASTBOOTD_ERASE_H
 
+#include <unistd.h>
+#include <errno.h>
+#include <string.h>
+#include "debug.h"
+
 int flash_find_entry(const char *, char *, size_t);
 int flash_erase(int fd);
 
@@ -50,10 +55,12 @@
     ssize_t len;
 
     while ((len = TEMP_FAILURE_RETRY(read(fd, (void *) &buffer[readcount], size - readcount))) > 0) {
-        readcount -= len;
+        readcount += len;
     }
-    if (len < 0)
+    if (len < 0) {
+        D(ERR, "Read error:%s", strerror(errno));
         return len;
+    }
 
     return readcount;
 }
diff --git a/fastbootd/fastbootd.c b/fastbootd/fastbootd.c
index 9318c99..2b51b33 100644
--- a/fastbootd/fastbootd.c
+++ b/fastbootd/fastbootd.c
@@ -38,17 +38,19 @@
 {
     int socket_client = 0;
     int c;
+    int network = 1;
 
     klog_init();
     klog_set_level(6);
 
     const struct option longopts[] = {
         {"socket", no_argument, 0, 'S'},
+        {"nonetwork", no_argument, 0, 'n'},
         {0, 0, 0, 0}
     };
 
     while (1) {
-        c = getopt_long(argc, argv, "S", longopts, NULL);
+        c = getopt_long(argc, argv, "Sn", longopts, NULL);
         /* Alphabetical cases */
         if (c < 0)
             break;
@@ -56,6 +58,9 @@
         case 'S':
             socket_client = 1;
             break;
+        case 'n':
+            network = 0;
+            break;
         case '?':
             return 1;
         default:
@@ -70,6 +75,7 @@
     klog_set_level(6);
 
     if (socket_client) {
+        //TODO: Shouldn't we change current tty into raw mode?
         run_socket_client();
     }
     else {
@@ -78,10 +84,14 @@
         load_trigger();
         commands_init();
         usb_init();
-        if (!transport_socket_init())
-            exit(1);
-        ssh_server_start();
-        network_discovery_init();
+
+        if (network) {
+            if (!transport_socket_init())
+                exit(1);
+            ssh_server_start();
+            network_discovery_init();
+        }
+
         while (1) {
             sleep(1);
         }
diff --git a/fastbootd/include/vendor_trigger.h b/fastbootd/include/vendor_trigger.h
index 66f65c4..51204fa 100644
--- a/fastbootd/include/vendor_trigger.h
+++ b/fastbootd/include/vendor_trigger.h
@@ -32,7 +32,7 @@
 #ifndef __VENDOR_TRIGGER_H_
 #define __VENDOR_TRIGGER_H_
 
-#define TRIGGER_MODULE_ID "vendortrigger"
+#define TRIGGER_MODULE_ID "fastbootd"
 #include <hardware/hardware.h>
 
 __BEGIN_DECLS
@@ -59,7 +59,7 @@
 
 
     /*
-     * Return value 1 forbid the action from the vendor site and sets errno
+     * Return value -1 forbid the action from the vendor site and sets errno
      */
     int (* gpt_layout)(struct GPT_content *);
     int (* oem_cmd)(const char *arg, const char **response);
diff --git a/fastbootd/other/vendor_trigger.c b/fastbootd/other/vendor_trigger.c
index 9b7562c..101959b 100644
--- a/fastbootd/other/vendor_trigger.c
+++ b/fastbootd/other/vendor_trigger.c
@@ -38,30 +38,11 @@
 
 static const int version = 1;
 
-int delete_partition(struct GPT_entry_raw *);
-int add_partition(struct GPT_entry_raw *);
-int modify_partition(struct GPT_entry_raw *, struct GPT_entry_raw *);
-
 int check_version(const int fastboot_version, int *libversion) {
     *libversion = version;
     return !(fastboot_version == version);
 }
 
-int delete_partition(struct GPT_entry_raw *entry) {
-    D(DEBUG, "message from libvendor");
-    return 0;
-}
-
-int add_partition(struct GPT_entry_raw *entry) {
-    D(DEBUG, "message from libvendor");
-    return 0;
-}
-
-int modify_partition(struct GPT_entry_raw *oldentry, struct GPT_entry_raw *newentry) {
-    D(DEBUG, "message from libvendor");
-    return 0;
-}
-
 int gpt_layout(struct GPT_content *table) {
     D(DEBUG, "message from libvendor");
     return 0;
diff --git a/fastbootd/secure.c b/fastbootd/secure.c
index 75a6f3c..a657ad4 100644
--- a/fastbootd/secure.c
+++ b/fastbootd/secure.c
@@ -108,6 +108,9 @@
         goto error;
     }
 
+    //TODO:
+    // read with d2i_CMS_bio to support DER
+    // with java or just encode data with base64
     *content = SMIME_read_CMS(input, output);
     if (*content == NULL) {
         unsigned long err = ERR_peek_last_error();
diff --git a/fastbootd/transport_socket.c b/fastbootd/transport_socket.c
index 801b8d6..ff0f3bd 100644
--- a/fastbootd/transport_socket.c
+++ b/fastbootd/transport_socket.c
@@ -136,13 +136,12 @@
 int transport_socket_init()
 {
     struct socket_transport *socket_transport = malloc(sizeof(struct socket_transport));
-    
+
     socket_transport->transport.connect = socket_connect;
     socket_transport->transport.close = socket_close;
     socket_transport->transport.read = socket_read;
     socket_transport->transport.write = socket_write;
-    // TODO: create sshd key pair if necessary
-    
+
     if (!listen_socket_init(socket_transport)) {
         D(ERR, "socket transport init failed");
         free(socket_transport);