merge in klp-release history after reset to master
diff --git a/CleanSpec.mk b/CleanSpec.mk
index 461aff4..d4024cf 100644
--- a/CleanSpec.mk
+++ b/CleanSpec.mk
@@ -181,6 +181,9 @@
 # Clean up around the /system/app -> /system/priv-app migration
 $(call add-clean-step, rm -rf $(PRODUCT_OUT)/system/app/*)
 
+# Clean up old location of generated Java files from aidl
+$(call add-clean-step, rm -rf $(OUT_DIR)/target/common/obj/JAVA_LIBRARIES/framework_intermediates/src)
+
 # ************************************************
 # NEWER CLEAN STEPS MUST BE AT THE END OF THE LIST
 # ************************************************
diff --git a/core/Makefile b/core/Makefile
index 55ab6a5..171936f 100644
--- a/core/Makefile
+++ b/core/Makefile
@@ -190,9 +190,14 @@
 			TARGET_CPU_ABI2="$(TARGET_CPU_ABI2)" \
 			TARGET_AAPT_CHARACTERISTICS="$(TARGET_AAPT_CHARACTERISTICS)" \
 	        bash $(BUILDINFO_SH) > $@
-	$(hide) if [ -f "$(system_prop_file)" ]; then \
-	          cat $(system_prop_file) >> $@; \
-	        fi
+	$(hide) $(foreach file,$(system_prop_file), \
+		if [ -f "$(file)" ]; then \
+			echo "#" >> $@; \
+			echo Target buildinfo from: "$(file)"; \
+			echo "# from $(file)" >> $@; \
+			echo "#" >> $@; \
+			cat $(file) >> $@; \
+		fi;)
 	$(if $(ADDITIONAL_BUILD_PROPERTIES), \
 		$(hide) echo >> $@; \
 		        echo "#" >> $@; \
@@ -1142,6 +1147,7 @@
 		$(INSTALLED_CACHEIMAGE_TARGET) \
 		$(INSTALLED_VENDORIMAGE_TARGET) \
 		$(INSTALLED_ANDROID_INFO_TXT_TARGET) \
+		$(SELINUX_FC) \
 		$(built_ota_tools) \
 		$(APKCERTS_FILE) \
 		$(HOST_OUT_EXECUTABLES)/fs_config \
@@ -1233,9 +1239,9 @@
 	@# Zip everything up, preserving symlinks
 	$(hide) (cd $(zip_root) && zip -qry ../$(notdir $@) .)
 	@# Run fs_config on all the system, boot ramdisk, and recovery ramdisk files in the zip, and save the output
-	$(hide) zipinfo -1 $@ | awk 'BEGIN { FS="SYSTEM/" } /^SYSTEM\// {print "system/" $$2}' | $(HOST_OUT_EXECUTABLES)/fs_config > $(zip_root)/META/filesystem_config.txt
-	$(hide) zipinfo -1 $@ | awk 'BEGIN { FS="BOOT/RAMDISK/" } /^BOOT\/RAMDISK\// {print $$2}' | $(HOST_OUT_EXECUTABLES)/fs_config > $(zip_root)/META/boot_filesystem_config.txt
-	$(hide) zipinfo -1 $@ | awk 'BEGIN { FS="RECOVERY/RAMDISK/" } /^RECOVERY\/RAMDISK\// {print $$2}' | $(HOST_OUT_EXECUTABLES)/fs_config > $(zip_root)/META/recovery_filesystem_config.txt
+	$(hide) zipinfo -1 $@ | awk 'BEGIN { FS="SYSTEM/" } /^SYSTEM\// {print "system/" $$2}' | $(HOST_OUT_EXECUTABLES)/fs_config -S $(SELINUX_FC) > $(zip_root)/META/filesystem_config.txt
+	$(hide) zipinfo -1 $@ | awk 'BEGIN { FS="BOOT/RAMDISK/" } /^BOOT\/RAMDISK\// {print $$2}' | $(HOST_OUT_EXECUTABLES)/fs_config -S $(SELINUX_FC) > $(zip_root)/META/boot_filesystem_config.txt
+	$(hide) zipinfo -1 $@ | awk 'BEGIN { FS="RECOVERY/RAMDISK/" } /^RECOVERY\/RAMDISK\// {print $$2}' | $(HOST_OUT_EXECUTABLES)/fs_config -S $(SELINUX_FC) > $(zip_root)/META/recovery_filesystem_config.txt
 	$(hide) (cd $(zip_root) && zip -q ../$(notdir $@) META/*filesystem_config.txt)
 
 .PHONY: target-files-package
diff --git a/core/build_id.mk b/core/build_id.mk
index 388b862..40bb35d 100644
--- a/core/build_id.mk
+++ b/core/build_id.mk
@@ -13,9 +13,20 @@
 # See the License for the specific language governing permissions and
 # limitations under the License.
 #
+
+#
+# Defines branch-specific values.
+#
+
 # BUILD_ID is usually used to specify the branch name
 # (like "MAIN") or a branch name and a release candidate
-# (like "CRB01").  It must be a single word, and is
+# (like "TC1-RC5").  It must be a single word, and is
 # capitalized by convention.
+#
+BUILD_ID := MASTER
 
-export BUILD_ID=KRS18C
+# DISPLAY_BUILD_NUMBER should only be set for development branches,
+# If set, the BUILD_NUMBER (cl) is appended to the BUILD_ID for
+# a more descriptive BUILD_ID_DISPLAY, otherwise BUILD_ID_DISPLAY
+# is the same as BUILD_ID
+DISPLAY_BUILD_NUMBER := true
diff --git a/core/clear_vars.mk b/core/clear_vars.mk
index 11f7f84..3aed193 100644
--- a/core/clear_vars.mk
+++ b/core/clear_vars.mk
@@ -129,6 +129,7 @@
 LOCAL_CLANG:=
 LOCAL_ADDRESS_SANITIZER:=
 LOCAL_JAR_EXCLUDE_FILES:=
+LOCAL_JAR_PACKAGES:=
 LOCAL_LINT_FLAGS:=
 LOCAL_SOURCE_FILES_ALL_GENERATED:= # '',true
 # Don't delete the META_INF dir when merging static Java libraries.
diff --git a/core/definitions.mk b/core/definitions.mk
index bf9901e..990e880 100644
--- a/core/definitions.mk
+++ b/core/definitions.mk
@@ -1511,6 +1511,10 @@
     -name $(word 1, $(PRIVATE_JAR_EXCLUDE_FILES)) \
     $(addprefix -o -name , $(wordlist 2, 999, $(PRIVATE_JAR_EXCLUDE_FILES))) \
     | xargs rm -rf)
+$(if $(PRIVATE_JAR_PACKAGES), $(hide) find $(PRIVATE_CLASS_INTERMEDIATES_DIR) -mindepth 1 -type d \
+    $(foreach pkg, $(PRIVATE_JAR_PACKAGES), \
+        -not -path $(PRIVATE_CLASS_INTERMEDIATES_DIR)/$(subst .,/,$(pkg))) \
+    | xargs rm -rf)
 $(hide) jar $(if $(strip $(PRIVATE_JAR_MANIFEST)),-cfm,-cf) \
     $@ $(PRIVATE_JAR_MANIFEST) -C $(PRIVATE_CLASS_INTERMEDIATES_DIR) .
 endef
@@ -1558,6 +1562,10 @@
     -name $(word 1, $(PRIVATE_JAR_EXCLUDE_FILES)) \
     $(addprefix -o -name , $(wordlist 2, 999, $(PRIVATE_JAR_EXCLUDE_FILES))) \
     | xargs rm -rf)
+$(if $(PRIVATE_JAR_PACKAGES), $(hide) find $(PRIVATE_CLASS_INTERMEDIATES_DIR) -mindepth 1 -type d \
+    $(foreach pkg, $(PRIVATE_JAR_PACKAGES), \
+        -not -path $(PRIVATE_CLASS_INTERMEDIATES_DIR)/$(subst .,/,$(pkg))) \
+    | xargs rm -rf)
 $(hide) jar $(if $(strip $(PRIVATE_JAR_MANIFEST)),-cfm,-cf) \
     $@ $(PRIVATE_JAR_MANIFEST) -C $(PRIVATE_CLASS_INTERMEDIATES_DIR) .
 $(hide) mv $(PRIVATE_CLASS_INTERMEDIATES_DIR)/newstamp $(PRIVATE_CLASS_INTERMEDIATES_DIR)/stamp
diff --git a/core/dex_preopt.mk b/core/dex_preopt.mk
index 8b2c868..ce27763 100644
--- a/core/dex_preopt.mk
+++ b/core/dex_preopt.mk
@@ -4,7 +4,7 @@
 ####################################
 
 # TODO: replace it with device's BOOTCLASSPATH
-DEXPREOPT_BOOT_JARS := core:conscrypt:okhttp:core-junit:bouncycastle:ext:framework:telephony-common:voip-common:mms-common:android.policy:services:apache-xml:webviewchromium
+DEXPREOPT_BOOT_JARS := core:conscrypt:okhttp:core-junit:bouncycastle:ext:framework:framework2:telephony-common:voip-common:mms-common:android.policy:services:apache-xml:webviewchromium
 DEXPREOPT_BOOT_JARS_MODULES := $(subst :, ,$(DEXPREOPT_BOOT_JARS))
 
 DEXPREOPT_BUILD_DIR := $(OUT_DIR)
diff --git a/core/droiddoc.mk b/core/droiddoc.mk
index 8a5c556..0d3094d 100644
--- a/core/droiddoc.mk
+++ b/core/droiddoc.mk
@@ -69,7 +69,7 @@
     LOCAL_JAVA_LIBRARIES := sdk_v$(LOCAL_SDK_VERSION) $(LOCAL_JAVA_LIBRARIES)
   endif
 else
-  LOCAL_JAVA_LIBRARIES := core ext framework $(LOCAL_JAVA_LIBRARIES)
+  LOCAL_JAVA_LIBRARIES := core ext framework framework2 $(LOCAL_JAVA_LIBRARIES)
 endif  # LOCAL_SDK_VERSION
 LOCAL_JAVA_LIBRARIES := $(sort $(LOCAL_JAVA_LIBRARIES))
 
diff --git a/core/java.mk b/core/java.mk
index 5315ccc..c7e93b7 100644
--- a/core/java.mk
+++ b/core/java.mk
@@ -43,7 +43,7 @@
   endif
 else
   ifneq ($(LOCAL_NO_STANDARD_LIBRARIES),true)
-    LOCAL_JAVA_LIBRARIES := core core-junit ext framework $(LOCAL_JAVA_LIBRARIES)
+    LOCAL_JAVA_LIBRARIES := core core-junit ext framework framework2 $(LOCAL_JAVA_LIBRARIES)
   endif
 endif
 
@@ -337,6 +337,7 @@
 # via deps on the target that generates the sources.
 $(full_classes_compiled_jar): PRIVATE_JAVACFLAGS := $(LOCAL_JAVACFLAGS)
 $(full_classes_compiled_jar): PRIVATE_JAR_EXCLUDE_FILES := $(LOCAL_JAR_EXCLUDE_FILES)
+$(full_classes_compiled_jar): PRIVATE_JAR_PACKAGES := $(LOCAL_JAR_PACKAGES)
 $(full_classes_compiled_jar): PRIVATE_DONT_DELETE_JAR_META_INF := $(LOCAL_DONT_DELETE_JAR_META_INF)
 $(full_classes_compiled_jar): $(java_sources) $(java_resource_sources) $(full_java_lib_deps) \
         $(jar_manifest_file) $(layers_file) $(RenderScript_file_stamp) \
diff --git a/core/pathmap.mk b/core/pathmap.mk
index f3a9e78..5dfc543 100644
--- a/core/pathmap.mk
+++ b/core/pathmap.mk
@@ -75,11 +75,6 @@
 # A list of all source roots under frameworks/base, which will be
 # built into the android.jar.
 #
-# Note - "common" is included here, even though it is also built
-# into a static library (android-common) for unbundled use.  This
-# is so common and the other framework libraries can have mutual
-# interdependencies.
-#
 FRAMEWORKS_BASE_SUBDIRS := \
 	$(addsuffix /java, \
 	    core \
diff --git a/core/pdk_config.mk b/core/pdk_config.mk
index 41a82d4..da55b38 100644
--- a/core/pdk_config.mk
+++ b/core/pdk_config.mk
@@ -47,6 +47,7 @@
 	target/common/obj/JAVA_LIBRARIES/core-junit_intermediates \
 	target/common/obj/JAVA_LIBRARIES/ext_intermediates \
 	target/common/obj/JAVA_LIBRARIES/framework_intermediates \
+	target/common/obj/JAVA_LIBRARIES/framework2_intermediates \
 	target/common/obj/JAVA_LIBRARIES/android.test.runner_intermediates \
 	target/common/obj/JAVA_LIBRARIES/telephony-common_intermediates \
 	target/common/obj/JAVA_LIBRARIES/voip-common_intermediates \
diff --git a/tools/droiddoc/templates-sdk/assets/css/default.css b/tools/droiddoc/templates-sdk/assets/css/default.css
index 91b0f59..d66cb6a 100644
--- a/tools/droiddoc/templates-sdk/assets/css/default.css
+++ b/tools/droiddoc/templates-sdk/assets/css/default.css
@@ -90,7 +90,7 @@
   outline:none;
   width:auto;
   margin: 20px 0 0; }
-  
+
 #devdoc-nav h2 {
   border:0;
 }
@@ -99,7 +99,7 @@
   position: fixed;
   margin:0;
   top: 20px; }
-  
+
 #devdoc-nav span.small {
   font-size:12px;
   font-weight:normal;
@@ -116,7 +116,7 @@
 a:focus,
 a:active {
   color: #33b5e5 !important; }
-  
+
 a.external-link {
   background:url('../images/styles/open_new_page.png') no-repeat 100% 50%;
   padding-right:16px;
@@ -338,10 +338,10 @@
       width: 34px;
       height: 34px; }
   #nav li.expanded li ul.tree-list-children {
-    padding:0;  
+    padding:0;
   }
   #nav li.expanded li ul.tree-list-children .tree-list-children {
-    padding:0 0 0 10px;  
+    padding:0 0 0 10px;
   }
   #nav li span.tree-list-subtitle {
     display:inline-block;
@@ -408,7 +408,7 @@
 .content-header.just-links {
   margin-bottom:0;
   padding-bottom:0;}
-    
+
 .content-header h1 {
   color:#000;
   margin:0;
@@ -470,8 +470,8 @@
       height: 10px;
       display: inline-block;
       margin-left: 5px; }
-      
-      
+
+
   .training-nav-top a {
     display:block;
     float:left;
@@ -488,25 +488,25 @@
     border-left:0;
     width:123px;
   }
-      
+
   .paging-links a.disabled,
   .training-nav-top a.disabled,
   .content-footer a.disabled {
     color:#bbb;
   }
-      
+
   .paging-links a.disabled:hover,
   .training-nav-top a.disabled:hover,
   .content-footer a.disabled:hover {
     cursor:default;
     color:#bbb !important;
   }
-      
+
   .training-nav-top a.start-class-link,
   .training-nav-top a.start-course-link {
     width:262px;
   }
-  
+
   /* list of classes on course landing page */
   ol.class-list {
     list-style:none;
@@ -549,7 +549,7 @@
   ol.class-list li a.title:hover span {
     display:block;
   }
-  
+
   #jd-content
   ol.class-list li img {
     float:left;
@@ -577,29 +577,29 @@
     margin:0 0 6px;
     line-height:16px;
   }
-  
-  
+
+
   .hide {
     display:none !important;
   }
-  
+
   .content-footer.next-class {
     display:block;
     border:0;
     margin-top:0;
     padding-top:0;
   }
-  
+
   .content-footer.next-class a.next-class-link {
     display:block;
     float:right;
     text-transform:uppercase;
   }
-  
-  
-  
+
+
+
   /* inner-doc tabs w/ title */
-  
+
 div#title-tabs-wrapper {
   border-bottom:1px solid #ccc;
   margin:20px 0 30px;
@@ -932,7 +932,7 @@
   .ui-table th,
   .ui-table td {
     padding: 5px 10px;
-    background-color: inherit; 
+    background-color: inherit;
     border:0;}
   .ui-table thead th {
     font-weight: bold; }
@@ -957,7 +957,7 @@
     background-image: url(../images/styles/ico_wrong.png); }
   .do-dont-label.good {
     background-image: url(../images/styles/ico_good.png); }
-    
+
 
 
 
@@ -1008,7 +1008,7 @@
 fieldset, iframe, img {
     border: 0;
 }
-img { 
+img {
   -ms-interpolation-mode: bicubic;
   vertical-align: middle;
   max-width: 100%;
@@ -1043,7 +1043,7 @@
   margin-right:320px;
 }
 h1.super {
-    font-size: 37px;  
+    font-size: 37px;
 }
 h2 {
     color:#333;
@@ -1064,13 +1064,13 @@
   font-size: 16px;
 }
 h5 {
-  font-size: 14px;  
+  font-size: 14px;
 }
 h5, h6 {
   margin: 5px 0;
 }
 h6 {
-  font-size: 12px;  
+  font-size: 12px;
 }
 hr { /* applied to the bottom of h2 elements */
   height: 1px;
@@ -1256,7 +1256,7 @@
     padding: 0 24px 0 0;
 }
 .locales form {
-    margin: 0;  
+    margin: 0;
 }
 .locales select, .sites select {
   line-height: 3.08;
@@ -1352,7 +1352,7 @@
   padding: 2.5em 0;
   border-top: solid 1px #ddd;
   border-bottom: solid 1px #ddd;
-  background: #f7f7f7;  
+  background: #f7f7f7;
 }
 .wrap {
   margin: 0 auto;
@@ -1478,7 +1478,7 @@
   top: 24px;
   left: 0
 }
-.search .left, .search .right { 
+.search .left, .search .right {
   height: 5px;
   width: 1px
 }
@@ -1549,7 +1549,7 @@
   padding: 0
 }
 .search-dropdown ul li {
-  clear: both 
+  clear: both
 }
 .search-dropdown img {
   float: left;
@@ -1682,7 +1682,7 @@
 endColorstr='#ffffff',GradientType=0 ); /* IE6-8 */
   -webkit-box-shadow: inset 0px 0px 5px 2px rgba(0, 0, 0, .05);
   -moz-box-shadow: inset 0px 0px 5px 2px rgba(0, 0, 0, .05);
-  box-shadow: inset 0px 0px 5px 2px rgba(0, 0, 0, .05); 
+  box-shadow: inset 0px 0px 5px 2px rgba(0, 0, 0, .05);
   color: #258AAF !important;
 }
 .button.big {
@@ -1715,7 +1715,7 @@
   width:130px;
   text-transform:uppercase;
   font-weight:bold;
-  
+
     background-color: #f3f3f3;
     background-image: -webkit-gradient(linear, left top, left bottom, from(#f9f9f9), to(#ececec));
     background-image: -webkit-linear-gradient(top, #f9f9f9, #ececec);
@@ -1973,9 +1973,9 @@
     padding: 5px;
     width: 130px;
 }
-    
+
 .article form .browse .browse-msg {
-  font-size: 11.5px;  
+  font-size: 11.5px;
 }
 .article form .browse .button-secondary {
   height: auto;
@@ -1990,7 +1990,7 @@
     color: #222;
     line-height: normal;
     padding: 6px 10px;
-    width: 300px; 
+    width: 300px;
 }
 .article form textarea {
     height: 150px;
@@ -2097,7 +2097,7 @@
   padding: 1em;
   overflow: auto;
   border: solid 1px #ddd;
-  background: #f7f7f7;  
+  background: #f7f7f7;
 }
 .str { color: #080; }
 .kwd { color: #008; }
@@ -2127,16 +2127,16 @@
         height:270px;
   max-height: inherit;
   overflow: hidden;
-  position: relative; 
+  position: relative;
 }
 #classes-nav {
   overflow: hidden;
-  position: relative; 
+  position: relative;
 }
 #packages-nav ul, #classes-nav ul {
   list-style-type: none;
   margin: 10px 0 20px 0;
-  padding: 0; 
+  padding: 0;
 }
 #classes-nav li {
   font-weight: bold;
@@ -2154,7 +2154,7 @@
 #classes-nav li li a, #classes-nav li li a:active, #classes-nav li li a:visited,
 #nav-tree li a, #nav-tree li a:active, #nav-tree li a:visited {
   color: #222;
-  font-weight: normal;  
+  font-weight: normal;
 }
 #packages-nav li a, #packages-nav li a:active, #packages-nav li a:visited,
 #classes-nav li li a, #classes-nav li li a:active, #classes-nav li li a:visited {
@@ -2200,7 +2200,7 @@
 #nav-swap .fullscreen.disabled {
   background-position: 0 0;
 }
-#nav-swap .fullscreen:hover, 
+#nav-swap .fullscreen:hover,
 #nav-swap .fullscreen:focus {
   cursor:pointer;
 }
@@ -2277,13 +2277,13 @@
   margin-right:0;
 }
 #doc-content-container {
-  margin-left: 291px  
+  margin-left: 291px
 }
 #doc-header, #doc-content {
   padding: 1em 2em;
 }
 #doc-header {
-  background: #f7f7f7;  
+  background: #f7f7f7;
 }
 #doc-header h1 {
   line-height: 0;
@@ -2411,7 +2411,7 @@
 border-bottom: solid 1px #ededed;
   background: #f7f7f7 url("../images/resizable-s2.png") no-repeat scroll center center; }
 /*
-.ui-resizable-e { 
+.ui-resizable-e {
 cursor: e-resize; width: 10px; right: 0; top: 0; height: 100%; border-right: solid
 1px #ededed;background: #f7f7f7 url("../images/resizable-e2.png") no-repeat scroll center center; }
 */
@@ -2419,7 +2419,7 @@
 /* --------------------------------------------------------------------------
 Lightbox
 */
-.lightbox { 
+.lightbox {
   width: 769px;
   padding: 1.5em;
   margin: 0 auto;
@@ -2432,7 +2432,7 @@
 .lightbox .header {
   float: left;
   width: 720px;
-  margin: -10px 20px 10px 0;  
+  margin: -10px 20px 10px 0;
 }
 .lightbox .close {
   float: right;
@@ -2492,7 +2492,7 @@
 .caption {
   margin: 0.5em 0 2em 0;
   color: #000;
-  font-size: 11.5px;  
+  font-size: 11.5px;
 }
 
 .nolist {
@@ -2551,19 +2551,19 @@
 }
 
 p.code-caption {
-  margin: 0 0 4px 0;
+  margin-bottom: 4px;
   font: 12px/1.5 monospace;
   color:#666;
 }
 
-div.note, 
-div.caution, 
+div.note,
+div.caution,
 div.warning {
   margin: 0 0 15px;
 }
 
-p.note, div.note, 
-p.caution, div.caution, 
+p.note, div.note,
+p.caution, div.caution,
 p.warning, div.warning {
   padding: 0 0 0 10px;
   border-left: 4px solid;
@@ -2719,12 +2719,12 @@
 
 
 /* -----------------------------------------------
-good/bad example containers 
+good/bad example containers
 */
 
 div.example-block {
   background-repeat: no-repeat;
-  background-position:10px 8px; 
+  background-position:10px 8px;
   background-color:#ccc;
   padding:4px;
   margin:.8em auto 1.5em 2em;
@@ -2758,7 +2758,7 @@
 }
 
 /* -----------------------------------------------
-Dialog box for popup messages 
+Dialog box for popup messages
 */
 
 div.dialog {
@@ -2824,9 +2824,9 @@
 
 
 /* --------------------------------------------------------------------------
-Slideshow Controls & Next/Prev 
+Slideshow Controls & Next/Prev
 */
-.slideshow-next, .slideshow-prev {  
+.slideshow-next, .slideshow-prev {
   width: 20px;
   height: 36px;
   text-indent: -1000em;
@@ -2849,7 +2849,7 @@
 
 a.slideshow-prev, a.slideshow-prev:visited {
 
-  float: left;  
+  float: left;
 
   background: url(../images/arrow-left.png) no-repeat 0 0
 
@@ -2857,18 +2857,18 @@
 
 .slideshow-next:hover, .slideshow-prev:hover, .slideshow-next:focus, .slideshow-prev:focus {
 
-  background-position: 0 -36px  
+  background-position: 0 -36px
 
 }
 
 .slideshow-next:active, .slideshow-prev:active {
 
-  background-position: 0 -72px  
+  background-position: 0 -72px
 
 }
 .slideshow-nav {
   width: 74px;
-  margin: 0 auto;   
+  margin: 0 auto;
 }
 .slideshow-nav a, .slideshow-nav a:visited {
   display: inline-block;
@@ -2888,7 +2888,7 @@
 .slideshow-nav a:active {
 
   background: #1e799a;
-  background: #ebebeb;  
+  background: #ebebeb;
   -webkit-box-shadow: inset 0px 0px 5px 2px rgba(0, 0, 0, .05);
   -moz-box-shadow: inset 0px 0px 5px 2px rgba(0, 0, 0, .05);
   box-shadow: inset 0px 0px 5px 2px rgba(0, 0, 0, .05);
@@ -2901,7 +2901,7 @@
 */
 ul.tabs {
   padding: 0;
-  margin: 2em 0 0 0;  
+  margin: 2em 0 0 0;
 }
 ul.tabs:before, ul.tabs:after {
   content: "";
@@ -2910,7 +2910,7 @@
 }
 ul.tabs li {
   list-style-type: none;
-  float: left;  
+  float: left;
 }
 ul.tabs li a, ul.tabs li a:active, ul.tabs li a:visited {
   display: block;
@@ -2924,7 +2924,7 @@
   -moz-border-radius-bottomright: px;
   -moz-border-radius-bottomleft: px;
   -webkit-border-radius: 2px 2px px px;
-  border-radius: 2px 2px px px; 
+  border-radius: 2px 2px px px;
   border-top: solid 1px #ebebeb;
   border-left: solid 1px #ebebeb;
   border-right: solid 1px #ebebeb;
@@ -2939,7 +2939,7 @@
 EndColorStr='#fafafa');
 }
 ul.tabs li a:hover {
-  color: #33B5E5; 
+  color: #33B5E5;
 }
 ul.tabs li a.selected {
   height: 37px;
@@ -2978,9 +2978,9 @@
 .dialog .box-border .top,
 .dialog .box-border .bottom { width:391px; }
 
-.box-border .left, .box-border .right { 
+.box-border .left, .box-border .right {
   width: 1px;
-  height: 8px;    
+  height: 8px;
 }
 .box-border .top { top: 0; left: 0 }
 .box-border .top .left { top: 1px; left: 0 }
@@ -3015,10 +3015,10 @@
     transition: color .4s ease;
 }
 .feature-box:hover {
-  cursor: pointer;  
+  cursor: pointer;
 }
 .feature-box:hover .box-border .top, .feature-box:hover .box-border .bottom, .feature-box:hover
-.left, .feature-box:hover .right {  
+.left, .feature-box:hover .right {
   background-color: #33B5E5;
 }
 .feature-box:hover h4, .feature-box:hover a {
@@ -3027,7 +3027,7 @@
 /* --------------------------------------------------------------------------
 Page-Specific Styles
 */
-.colors { 
+.colors {
   position: relative;
   float: left;
   width: 92px;
@@ -3446,7 +3446,7 @@
   font-size:0.85em;
 }
 
-.suggest-card.reference 
+.suggest-card.reference
 .search_filtered li.header {
   color:#aaa;
   font-size: 0.81em;
@@ -3797,7 +3797,7 @@
 }
 
 #quicknav {
- float:none; 
+ float:none;
  clear:both;
  margin-left:180px;
  margin-top:-30px;
@@ -3865,7 +3865,7 @@
 
 #header-wrap.quicknav {
   height:196px;
-  
+
 }
 
 /* SEARCH AND MORE */
@@ -3912,7 +3912,7 @@
   left: 0
 }
 
-.search .left, .search .right { 
+.search .left, .search .right {
   height: 5px;
   width: 1px
 }
@@ -3938,10 +3938,10 @@
 .search form input {
   color: #2f2f2f;
   font-size: 0.95em;
-  width: 178px;  
+  width: 178px;
   border: none;
-  margin-left: 6px;  
-  z-index: 1500;  
+  margin-left: 6px;
+  z-index: 1500;
   position: relative;
   background-color: transparent;
   border-bottom:1px solid #CCC;
@@ -4010,10 +4010,10 @@
           opacity:0;
 
   -webkit-transform-origin:100% 0%;
-     -moz-transform-origin:100% 0%; 
+     -moz-transform-origin:100% 0%;
        -o-transform-origin:100% 0%;
           transform-origin:100% 0%;
-  
+
   -webkit-transition-property: -webkit-opacity;
   -webkit-transition-duration: .25s;
   -webkit-transition-timing-function:ease;
@@ -4025,7 +4025,7 @@
   -o-transition-property: -o-opacity;
   -o-transition-duration: .25s;
   -o-transition-timing-function:ease;
-  
+
   transition-property: opacity;
   transition-duration: .25s;
   transition-timing-function:ease;
@@ -4123,9 +4123,9 @@
   border-radius:6px;
   background-color:#ccc;
   cursor:pointer;
-        -webkit-transition:color .5s ease-in;  
-        -moz-transition:color .5s ease-in;  
-        -o-transition:color .5s ease-in;  
+        -webkit-transition:color .5s ease-in;
+        -moz-transition:color .5s ease-in;
+        -o-transition:color .5s ease-in;
         transition:color .5s ease-in;
 }
 .slideshow-container .pagination ul li:hover {
@@ -4261,7 +4261,7 @@
   background-image: -ms-linear-gradient(left, #5dbcd9, #4cadcb);
   background-image: -o-linear-gradient(left, #5dbcd9, #4cadcb);
   background-image: linear-gradient(left, #5dbcd9, #4cadcb);
-  filter: progid:DXImageTransform.Microsoft.gradient(startColorStr='#5dbcd9', EndColorStr='#4cadcb'); 
+  filter: progid:DXImageTransform.Microsoft.gradient(startColorStr='#5dbcd9', EndColorStr='#4cadcb');
 }
 
 .jspHorizontalBar .jspTrack,
diff --git a/tools/fs_config/Android.mk b/tools/fs_config/Android.mk
index 5ef32dd..02deabb 100644
--- a/tools/fs_config/Android.mk
+++ b/tools/fs_config/Android.mk
@@ -17,6 +17,7 @@
 
 LOCAL_SRC_FILES := fs_config.c
 LOCAL_MODULE := fs_config
+LOCAL_STATIC_LIBRARIES := libselinux
 LOCAL_FORCE_STATIC_EXECUTABLE := true
 
 include $(BUILD_HOST_EXECUTABLE)
diff --git a/tools/fs_config/fs_config.c b/tools/fs_config/fs_config.c
index f6760cc..60c3238 100644
--- a/tools/fs_config/fs_config.c
+++ b/tools/fs_config/fs_config.c
@@ -15,11 +15,16 @@
  */
 
 #include <stdio.h>
+#include <stdlib.h>
 #include <sys/stat.h>
 #include <errno.h>
 #include <unistd.h>
 #include <string.h>
 
+#include <selinux/selinux.h>
+#include <selinux/label.h>
+#include <selinux/android.h>
+
 #include "private/android_filesystem_config.h"
 
 // This program takes a list of files and directories (indicated by a
@@ -29,19 +34,56 @@
 //
 // Example input:
 //
-//    system/etc/dbus.conf
-//    data/app/
+//      system/etc/dbus.conf
+//      data/app/
 //
 // Output:
 //
-//    system/etc/dbus.conf 1002 1002 440
-//    data/app 1000 1000 771
+//      system/etc/dbus.conf 1002 1002 440
+//      data/app 1000 1000 771
+//
+//   or if -S is used:
+//
+//      system/etc/dbus.conf 1002 1002 440 u:object_r:system_file:s0
+//      data/app 1000 1000 771 u:object_r:apk_data_file:s0
 //
 // Note that the output will omit the trailing slash from
 // directories.
 
+static struct selabel_handle* get_sehnd(const char* context_file) {
+  struct selinux_opt seopts[] = { { SELABEL_OPT_PATH, context_file } };
+  struct selabel_handle* sehnd = selabel_open(SELABEL_CTX_FILE, seopts, 1);
+
+  if (!sehnd) {
+    perror("error running selabel_open");
+    exit(EXIT_FAILURE);
+  }
+  return sehnd;
+}
+
+static void usage() {
+  fprintf(stderr, "Usage: fs_config [-S context_file]\n");
+}
+
 int main(int argc, char** argv) {
   char buffer[1024];
+  const char* context_file = NULL;
+  struct selabel_handle* sehnd = NULL;
+  int opt;
+  while((opt = getopt(argc, argv, "S:")) != -1) {
+    switch(opt) {
+    case 'S':
+      context_file = optarg;
+      break;
+    default:
+      usage();
+      exit(EXIT_FAILURE);
+    }
+  }
+
+  if (context_file != NULL) {
+    sehnd = get_sehnd(context_file);
+  }
 
   while (fgets(buffer, 1023, stdin) != NULL) {
     int is_dir = 0;
@@ -64,7 +106,35 @@
     unsigned uid = 0, gid = 0, mode = 0;
     uint64_t capabilities;
     fs_config(buffer, is_dir, &uid, &gid, &mode, &capabilities);
-    printf("%s %d %d %o\n", buffer, uid, gid, mode);
+    printf("%s %d %d %o", buffer, uid, gid, mode);
+
+    if (sehnd != NULL) {
+      size_t buffer_strlen = strnlen(buffer, sizeof(buffer));
+      if (buffer_strlen >= sizeof(buffer)) {
+        fprintf(stderr, "non null terminated buffer, aborting\n");
+        exit(EXIT_FAILURE);
+      }
+      size_t full_name_size = buffer_strlen + 2;
+      char* full_name = (char*) malloc(full_name_size);
+      if (full_name == NULL) {
+        perror("malloc");
+        exit(EXIT_FAILURE);
+      }
+
+      full_name[0] = '/';
+      strncpy(full_name + 1, buffer, full_name_size - 1);
+      full_name[full_name_size - 1] = '\0';
+
+      char* secontext;
+      if (selabel_lookup(sehnd, &secontext, full_name, ( mode | (is_dir ? S_IFDIR : S_IFREG)))) {
+        secontext = strdup("u:object_r:unlabeled:s0");
+      }
+
+      printf(" %s", secontext);
+      free(full_name);
+      freecon(secontext);
+    }
+    printf("\n");
   }
   return 0;
 }
diff --git a/tools/releasetools/edify_generator.py b/tools/releasetools/edify_generator.py
index 9ef1926..cafdeb3 100644
--- a/tools/releasetools/edify_generator.py
+++ b/tools/releasetools/edify_generator.py
@@ -217,14 +217,14 @@
       else:
         raise ValueError("don't know how to write \"%s\" partitions" % (p.fs_type,))
 
-  def SetPermissions(self, fn, uid, gid, mode):
+  def SetPermissions(self, fn, uid, gid, mode, secontext):
     """Set file ownership and permissions."""
-    self.script.append('set_perm(%d, %d, 0%o, "%s");' % (uid, gid, mode, fn))
+    self.script.append('set_perm2(%d, %d, 0%o, "%s", "%s");' % (uid, gid, mode, secontext, fn))
 
-  def SetPermissionsRecursive(self, fn, uid, gid, dmode, fmode):
+  def SetPermissionsRecursive(self, fn, uid, gid, dmode, fmode, secontext):
     """Recursively set path ownership and permissions."""
-    self.script.append('set_perm_recursive(%d, %d, 0%o, 0%o, "%s");'
-                       % (uid, gid, dmode, fmode, fn))
+    self.script.append('set_perm2_recursive(%d, %d, 0%o, 0%o, "%s", "%s");'
+                       % (uid, gid, dmode, fmode, secontext, fn))
 
   def MakeSymlinks(self, symlink_list):
     """Create symlinks, given a list of (dest, link) pairs."""
diff --git a/tools/releasetools/ota_from_target_files b/tools/releasetools/ota_from_target_files
index 1e1d04e..e79c13f 100755
--- a/tools/releasetools/ota_from_target_files
+++ b/tools/releasetools/ota_from_target_files
@@ -117,6 +117,7 @@
     self.uid = None
     self.gid = None
     self.mode = None
+    self.secontext = None
     self.dir = dir
 
     if name:
@@ -166,63 +167,66 @@
 
     for line in output.split("\n"):
       if not line: continue
-      name, uid, gid, mode = line.split()
+      name, uid, gid, mode, secontext = line.split()
       i = cls.ITEMS.get(name, None)
       if i is not None:
         i.uid = int(uid)
         i.gid = int(gid)
         i.mode = int(mode, 8)
+        i.secontext = secontext
         if i.dir:
           i.children.sort(key=lambda i: i.name)
 
     # set metadata for the files generated by this script.
     i = cls.ITEMS.get("system/recovery-from-boot.p", None)
-    if i: i.uid, i.gid, i.mode = 0, 0, 0644
+    if i: i.uid, i.gid, i.mode, i.secontext = 0, 0, 0644, "u:object_r:system_file:s0"
     i = cls.ITEMS.get("system/etc/install-recovery.sh", None)
-    if i: i.uid, i.gid, i.mode = 0, 0, 0544
+    if i: i.uid, i.gid, i.mode, i.secontext = 0, 0, 0544, "u:object_r:system_file:s0"
 
   def CountChildMetadata(self):
-    """Count up the (uid, gid, mode) tuples for all children and
+    """Count up the (uid, gid, mode, secontext) tuples for all children and
     determine the best strategy for using set_perm_recursive and
     set_perm to correctly chown/chmod all the files to their desired
     values.  Recursively calls itself for all descendants.
 
-    Returns a dict of {(uid, gid, dmode, fmode): count} counting up
+    Returns a dict of {(uid, gid, dmode, fmode, secontext): count} counting up
     all descendants of this node.  (dmode or fmode may be None.)  Also
     sets the best_subtree of each directory Item to the (uid, gid,
-    dmode, fmode) tuple that will match the most descendants of that
+    dmode, fmode, secontext) tuple that will match the most descendants of that
     Item.
     """
 
     assert self.dir
-    d = self.descendants = {(self.uid, self.gid, self.mode, None): 1}
+    d = self.descendants = {(self.uid, self.gid, self.mode, None, self.secontext): 1}
     for i in self.children:
       if i.dir:
         for k, v in i.CountChildMetadata().iteritems():
           d[k] = d.get(k, 0) + v
       else:
-        k = (i.uid, i.gid, None, i.mode)
+        k = (i.uid, i.gid, None, i.mode, i.secontext)
         d[k] = d.get(k, 0) + 1
 
-    # Find the (uid, gid, dmode, fmode) tuple that matches the most
+    # Find the (uid, gid, dmode, fmode, secontext) tuple that matches the most
     # descendants.
 
     # First, find the (uid, gid) pair that matches the most
     # descendants.
     ug = {}
-    for (uid, gid, _, _), count in d.iteritems():
+    for (uid, gid, _, _, _), count in d.iteritems():
       ug[(uid, gid)] = ug.get((uid, gid), 0) + count
     ug = MostPopularKey(ug, (0, 0))
 
-    # Now find the dmode and fmode that match the most descendants
+    # Now find the dmode, fmode and secontext that match the most descendants
     # with that (uid, gid), and choose those.
     best_dmode = (0, 0755)
     best_fmode = (0, 0644)
+    best_secontext = (0, "u:object_r:system_file:s0")
     for k, count in d.iteritems():
       if k[:2] != ug: continue
       if k[2] is not None and count >= best_dmode[0]: best_dmode = (count, k[2])
       if k[3] is not None and count >= best_fmode[0]: best_fmode = (count, k[3])
-    self.best_subtree = ug + (best_dmode[1], best_fmode[1])
+      if k[4] is not None and count >= best_secontext[0]: best_secontext = (count, k[4])
+    self.best_subtree = ug + (best_dmode[1], best_fmode[1], best_secontext[1])
 
     return d
 
@@ -234,7 +238,7 @@
     self.CountChildMetadata()
 
     def recurse(item, current):
-      # current is the (uid, gid, dmode, fmode) tuple that the current
+      # current is the (uid, gid, dmode, fmode, secontext) tuple that the current
       # item (and all its children) have already been set to.  We only
       # need to issue set_perm/set_perm_recursive commands if we're
       # supposed to be something different.
@@ -244,17 +248,17 @@
           current = item.best_subtree
 
         if item.uid != current[0] or item.gid != current[1] or \
-           item.mode != current[2]:
-          script.SetPermissions("/"+item.name, item.uid, item.gid, item.mode)
+           item.mode != current[2] or item.secontext != current[4]:
+          script.SetPermissions("/"+item.name, item.uid, item.gid, item.mode, item.secontext)
 
         for i in item.children:
           recurse(i, current)
       else:
         if item.uid != current[0] or item.gid != current[1] or \
-               item.mode != current[3]:
-          script.SetPermissions("/"+item.name, item.uid, item.gid, item.mode)
+               item.mode != current[3] or item.secontext != current[4]:
+          script.SetPermissions("/"+item.name, item.uid, item.gid, item.mode, item.secontext)
 
-    recurse(self, (-1, -1, -1, -1))
+    recurse(self, (-1, -1, -1, -1, "u:object_r:unlabeled:s0"))
 
 
 def CopySystemFiles(input_zip, output_zip=None,
@@ -733,7 +737,7 @@
   for item in deferred_patch_list:
     fn, tf, sf, size, _ = item
     script.ApplyPatch("/"+fn, "-", tf.size, tf.sha1, sf.sha1, "patch/"+fn+".p")
-  script.SetPermissions("/system/build.prop", 0, 0, 0644)
+  script.SetPermissions("/system/build.prop", 0, 0, 0644, "u:object_r:system_file:s0")
 
   script.AddToZip(target_zip, output_zip)
   WriteMetadata(metadata, output_zip)