firmware: start implementing open

Start implementing the open command for the firmware handler.

Change-Id: I8426e397b182bfe76625918a4148b1b6d1003f43
Signed-off-by: Patrick Venture <venture@google.com>
diff --git a/firmware_handler.cpp b/firmware_handler.cpp
index 41bf853..eccac73 100644
--- a/firmware_handler.cpp
+++ b/firmware_handler.cpp
@@ -97,20 +97,79 @@
     return false;
 }
 
+/*
+ * If you open /flash/image or /flash/tarball, or /flash/hash it will
+ * interpret the open flags and perform whatever actions are required for
+ * that update process.  The session returned can be used immediately for
+ * sending data down, without requiring one to open the new active file.
+ *
+ * If you open the active flash image or active hash it will let you
+ * overwrite pieces, depending on the state.
+ *
+ * Once the verification process has started the active files cannot be
+ * opened.
+ *
+ * You can only have one open session at a time.  Which means, you can only
+ * have one file open at a time.  Trying to open the hash blob_id while you
+ * still have the flash image blob_id open will fail.  Opening the flash
+ * blob_id when it is already open will fail.
+ */
 bool FirmwareBlobHandler::open(uint16_t session, uint16_t flags,
                                const std::string& path)
 {
-    /*
-     * If you open /flash/image or /flash/tarball, or /flash/hash it will
-     * interpret the open flags and perform whatever actions are required for
-     * that update process.  The session returned can be used immediately for
-     * sending data down, without requiring one to open the new active file.
-     *
-     * If you open the active flash image or active hash it will let you
-     * overwrite pieces, depending on the state.
-     * Once the verification process has started the active files cannot be
-     * opened.
+    /* Check that they've opened for writing - read back not supported. */
+    if ((flags & OpenFlags::write) == 0)
+    {
+        return false;
+    }
+
+    /* TODO: Is the verification process underway? */
+
+    /* Is there an open session already? We only allow one at a time.
+     * TODO: Temporarily using a simple boolean flag until there's a full
+     * session object to check.
      */
+    if (fileOpen)
+    {
+        return false;
+    }
+
+    /* There are two abstractions at play, how you get the data and how you
+     * handle that data. such that, whether the data comes from the PCI bridge
+     * or LPC bridge is not connected to whether the data goes into a static
+     * layout flash update or a UBI tarball.
+     */
+
+    /* Check the flags for the transport mechanism: if none match we don't
+     * support what they request. */
+    if ((flags & transports) == 0)
+    {
+        return false;
+    }
+
+    /* 2) there isn't, so what are they opening? */
+    if (path == activeImageBlobID)
+    {
+        /* 2a) are they opening the active image? this can only happen if they
+         * already started one (due to canHandleBlob's behavior). */
+    }
+    else if (path == activeHashBlobID)
+    {
+        /* 2b) are they opening the active hash? this can only happen if they
+         * already started one (due to canHandleBlob's behavior). */
+    }
+    else if (path == hashBlobID)
+    {
+        /* 2c) are they opening the /flash/hash ? (to start the process) */
+    }
+    else
+    {
+        /* 2d) are they opening the /flash/tarball ? (to start the UBI process)
+         */
+        /* 2e) are they opening the /flash/image ? (to start the process) */
+        /* 2...) are they opening the /flash/... ? (to start the process) */
+    }
+
     return false;
 }
 
diff --git a/firmware_handler.hpp b/firmware_handler.hpp
index 9d57b63..18b04fc 100644
--- a/firmware_handler.hpp
+++ b/firmware_handler.hpp
@@ -72,8 +72,14 @@
     static const std::string activeHashBlobID;
 
   private:
+    /** Active list of blobIDs. */
     std::vector<std::string> blobIDs;
+
+    /** The bits set indicate what transport mechanisms are supported. */
     std::uint16_t transports;
+
+    /** Temporary variable to track whether a blob is open. */
+    bool fileOpen = false;
 };
 
 } // namespace blobs