recovery: erase nanoapps on factory reset

Erase nanoapps in PostWipeData()

avc:  denied  { search } for  pid=312 comm="recovery" name="nanohub" dev="sysfs" ino=18838 scontext=u:r:recovery:s0 tcontext=u:object_r:sysfs_nanoapp_cmd:s0 tclass=dir permissive=0
avc:  denied  { open } for  pid=312 comm="recovery" path="/sys/devices/virtual/nanohub/nanohub/erase_shared" dev="sysfs" ino=18868 scontext=u:r:recovery:s0 tcontext=u:object_r:sysfs_nanoapp_cmd:s0 tclass=file permissive=0
avc:  denied  { write } for  pid=313 comm="recovery" name="erase_shared" dev="sysfs" ino=18868 scontext=u:r:recovery:s0 tcontext=u:object_r:sysfs_nanoapp_cmd:s0 tclass=file permissive=0

Bug: 28868735
Change-Id: I7059a7fda7a2b15837121e5cf08463c2ecb0879e
Signed-off-by: Ben Fennema <fennema@google.com>
diff --git a/BoardConfig.mk b/BoardConfig.mk
index 7e4be44..f003696 100644
--- a/BoardConfig.mk
+++ b/BoardConfig.mk
@@ -109,6 +109,8 @@
 
 TARGET_USES_INTERACTION_BOOST := true
 
+TARGET_RECOVERY_UI_LIB := librecovery_ui_nanohub
+
 # Force camera module to be compiled only in 32-bit mode on 64-bit systems
 # Once camera module can run in the native mode of the system (either
 # 32-bit or 64-bit), the following line should be deleted
diff --git a/recovery/Android.mk b/recovery/Android.mk
new file mode 100644
index 0000000..128d5e0
--- /dev/null
+++ b/recovery/Android.mk
@@ -0,0 +1,17 @@
+#
+# Copyright 2016 The Android Open Source Project
+#
+# Licensed under the Apache License, Version 2.0 (the "License");
+# you may not use this file except in compliance with the License.
+# You may obtain a copy of the License at
+#
+#      http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+#
+
+include $(all-subdir-makefiles)
diff --git a/recovery/nanohub/Android.mk b/recovery/nanohub/Android.mk
new file mode 100644
index 0000000..e3819d2
--- /dev/null
+++ b/recovery/nanohub/Android.mk
@@ -0,0 +1,30 @@
+#
+# Copyright 2016 The Android Open Source Project
+#
+# Licensed under the Apache License, Version 2.0 (the "License");
+# you may not use this file except in compliance with the License.
+# You may obtain a copy of the License at
+#
+#      http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+#
+
+ifeq ($(TARGET_RECOVERY_UI_LIB),librecovery_ui_nanohub)
+LOCAL_PATH := $(call my-dir)
+include $(CLEAR_VARS)
+
+LOCAL_MODULE := librecovery_ui_nanohub
+
+LOCAL_MODULE_TAGS := optional
+
+LOCAL_C_INCLUDES += bootable/recovery
+
+LOCAL_SRC_FILES := nanohub_recovery_ui.cpp
+
+include $(BUILD_STATIC_LIBRARY)
+endif
diff --git a/recovery/nanohub/nanohub_recovery_ui.cpp b/recovery/nanohub/nanohub_recovery_ui.cpp
new file mode 100644
index 0000000..6eeb750
--- /dev/null
+++ b/recovery/nanohub/nanohub_recovery_ui.cpp
@@ -0,0 +1,57 @@
+/*
+ * Copyright (C) 2016 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#include <sys/types.h>
+#include <sys/stat.h>
+#include <fcntl.h>
+#include <unistd.h>
+#include <errno.h>
+
+#include "device.h"
+#include "screen_ui.h"
+
+class Nanohub_Device : public Device
+{
+public:
+    Nanohub_Device(ScreenRecoveryUI* ui) : Device(ui) {}
+    bool PostWipeData();
+};
+
+bool Nanohub_Device::PostWipeData()
+{
+    int fd;
+
+    fd = open("/sys/class/nanohub/nanohub/erase_shared", O_WRONLY);
+    if (fd < 0) {
+        printf("error: open erase_shared failed: %s\n", strerror(errno));
+    } else {
+        if (write(fd, "1\n", 2) != 2) {
+            printf("error: write to erase_shared failed: %s\n", strerror(errno));
+        } else {
+            printf("Successfully erased nanoapps.\n");
+        }
+        close(fd);
+    }
+
+    // open/write failure caused by permissions issues would persist across
+    // reboots, so always return true to prevent a factory reset failure loop.
+    return true;
+}
+
+Device *make_device()
+{
+    return new Nanohub_Device(new ScreenRecoveryUI);
+}
diff --git a/sepolicy/recovery.te b/sepolicy/recovery.te
new file mode 100644
index 0000000..62fdc59
--- /dev/null
+++ b/sepolicy/recovery.te
@@ -0,0 +1,4 @@
+recovery_only(`
+  allow recovery sysfs_nanoapp_cmd:dir search;
+  allow recovery sysfs_nanoapp_cmd:file { open write };
+')